如何使用-Rails 4在post / put simple_form中传递id

时间:2015-07-09 23:52:15

标签: ruby forms ruby-on-rails-4

我是铁杆新手,所以提供任何帮助都会非常感激。

目标:

  

当用户在表单上留下注释(注释)时,i   希望能够知道userr_id& form_id就在那张纸条上。一世   我不确定如何将form_id链接到注释。当我创建一个注释时,它会告诉我哪个用户(userr_id)创建了它但它没有告诉我该注释属于哪个表单(form_id) - 任何帮助都会非常感激 - 谢谢

我目前有以下模型和视图创建

  • [model] note belongs_to:userj
  • [model] note belongs_to:form
  • [model] form has_many:notes
  • [model] userj has_many:notes

我的控制台:

2.1.2 :062 > ap Note.all
  Note Load (0.4ms)  SELECT "notes".* FROM "notes"
[
    [0] #<Note:0x007fbde666cd70> {
                :id => 4,
           :content => "hello",
           :form_id => nil,
        :created_at => Thu, 09 Jul 2015 23:17:19 UTC +00:00,
        :updated_at => Thu, 09 Jul 2015 23:17:19 UTC +00:00,
          :userr_id => 1
    }
]

模型:

note.rb
class Note < ActiveRecord::Base
  belongs_to :userj
  belongs_to :form
end

form.rb
class Form < ActiveRecord::Base
  has_many :notes
end

userr.rb
class Userr < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :notes
end

形式/ show.html.erb

<p>
  <strong>Firstname:</strong>
  <%= @form.firstname %>
</p>

<p>
  <strong>Lastname:</strong>
  <%= @form.lastname %>
</p>

<p>
  <strong>Number:</strong>
  <%= @form.number %>
</p>

<p>
  <strong>Email:</strong>
  <%= @form.email %>
</p>


<h2> Recruiters Notes on jobseeker form</h2>
<p>
  <%= render 'notes/form' %>
</p>

笔记/ _form.html.erb

  

我尝试按照下面的方式传递form_id,但是当我创建一个注释时   一张表格,它没有告诉我这张纸币属于哪种形式   form_id列(在注释表中)在控制台中指出nill

<%= simple_form_for(@note) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :content %>
    <%= f.hidden_field :form_id, value: params[:form_id] %>
  </div>

  <div class="form-actions">
    <%# f.button :submit %>
    <%= f.button  :submit , name: "form", value: params[:form_id] %>
  </div>
<% end %>

notes_controller.rb

class NotesController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_note, only: [:show, :edit, :update, :destroy]

  def index
    @notes = Note.all
    respond_with(@notes)
  end

  def show
    respond_with(@note)
  end

  def new
    @note = Note.new
    respond_with(@note)
  end

  def edit
  end

  def create
    @note = Note.new(note_params)
    @note.userr_id = current_userr.id
    @note.save
    respond_with(@note)
  end

  def update
    @note.update(note_params)
    respond_with(@note)
  end

  def destroy
    @note.destroy
    respond_with(@note)
  end

  private
    def set_note
      @note = Note.find(params[:id])
    end

    def note_params
      params.require(:note).permit(:content, :form_id, :userr_id)
    end
end

forms_controller.rb

class FormsController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_form, only: [:show, :edit, :update, :destroy]

  def index
    ...
  end

  def show
    @note = Note.new
    respond_with(@form)
  end

  def new
    ...
  end

  def edit
  end

  def create
    @form = Form.new(form_params)
    @form.userj_id = current_userj.id
    if @form.save
      MailerFormsubmission.form_submission_confirmation(@form).deliver
      redirect_to application_submitted_path
    else
      redirect_to userr_advert_path(advert.userr, advert)
    end
    # respond_with(@form)
  end

  def update
    ...
  end

  def destroy
    ...
  end

  private
    def set_form
      @form = Form.find(params[:id])
    end

    def form_params
     ....
    end
end

1 个答案:

答案 0 :(得分:1)

通常你不应该尝试在视图中使用params。在这种情况下,您可以:

  1. 通过与@note中的表单(@note = @form.notes.build)的关联构建FormsController#show实例变量,并在表单中包含隐藏字段;
  2. @form实例传递给partial,以便它可以使用以下格式:
  3. 形式/ show.html.erb:

        # ...
        <%= render partial: 'notes/form', locals: { form: @form } %>
    

    笔记/ _form.html.erb

        # ...
        <%= f.hidden_field :form_id, value: form.id %>