i following tutorial
trying upload file using paperclip gem ..when try upload file error method not allowed thats why assets table blank no data inserting
[asset.rb]
class asset < activerecord::base belongs_to :user has_attached_file :uploaded_file validates_attachment_size :uploaded_file, :less_than => 10.megabytes validates_attachment_presence :uploaded_file def file_name uploaded_file_file_name end def file_size uploaded_file_file_size end end
[user.rb]
class user < activerecord::base # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable validates :email, :presence => true, :uniqueness => true has_many :assets end
[assets_controller]
class assetscontroller < applicationcontroller before_filter :authenticate_user! #authenticate users before methods called def index @assets = current_user.assets end def show @asset = current_user.assets.find(params[:id]) end def new @asset = current_user.assets.new end def create @asset = current_user.assets.new(user_assets) if @asset.save flash[:notice] = "successfully uploaded file." else render :action => 'new' end end def edit @asset = current_user.assets.find(params[:id]) end def update @asset = current_user.assets.find(params[:id]) end def destroy @asset = current_user.assets.find(params[:id]) end private def user_assets params.require(:asset).permit(:user_id, :uploaded_file) end end <br>
[assets/_form.html.erb]
<%= form_for @asset, :html => {:multipart => true} |f| %> <%= f.error_messages %> <p> <%= f.label :uploaded_file, "file" %><br /> <%= f.file_field :uploaded_file %> </p> <p><%= f.submit "upload" %></p> <% end %>
[assets/new.html.erb]
<% title "upload file" %> <%= render 'form' %> <p> <%= link_to "back", root_url %> </p>
3[migration]
class createassets < activerecord::migration def change create_table :assets |t| t.integer :user_id t.timestamps end add_index :assets, :user_id end end
4[migration]
class addattachmentuploadedfiletoassets < activerecord::migration def change change_table :assets |t| add_column :assets, :uploaded_file_file_name, :string add_column :assets, :uploaded_file_content_type, :string add_column :assets, :uploaded_file_file_size, :integer add_column :assets, :uploaded_file_updated_at, :datetime end end end
]4
the problem op referencing /assets
reserved rails public path.
the solution therefore change at least routes other /assets
; model
can remain controller have change too:
#config/routes.rb resources :assets, path: "asset", only: [:new, :create] #-> url.com/asset/new
Comments
Post a Comment