使用form_for在rails上的ruby中命名参数

时间:2016-12-23 19:20:09

标签: ruby-on-rails parameters

我试图在rails中使用form_for传递一些参数,如何传递它们以便可以在控制器中访问它们:params[:image]?我的form_for标记的开头如下所示:

<%= form_for(@image, as: :image, url: create_image_path(current_user),  html: { multipart: true }, method: :post) do |f| %>

1 个答案:

答案 0 :(得分:0)

我可以给你一个载波的例子

gem install carrierwave

rails g model Resume  attachment:string

rails g uploader attachment

**在模型,控制器和视图中添加**

class Resume < ActiveRecord::Base
   mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.

end



class ResumesController < ApplicationController
   def index
      @resumes = Resume.all
   end

   def new
      @resume = Resume.new
   end

   def create
      @resume = Resume.new(resume_params)

      if @resume.save
         redirect_to resumes_path, notice: "The resume #{@resume.name} has been uploaded."
      else
         render "new"
      end

   end

   def destroy
      @resume = Resume.find(params[:id])
      @resume.destroy
      redirect_to resumes_path, notice:  "The resume #{@resume.name} has been deleted."
   end

   private
      def resume_params
      params.require(:resume).permit( :attachment)
   end

end


<div class = "well">
   <%= form_for @resume, html: { multipart: true } do |f| %>
      <%= f.file_field :attachment %>
      <%= f.submit "Save", class: "btn btn-primary" %>
   <% end %>
</div>