ฉันจะส่งรหัสในโพสต์ / วาง simple_form ด้วย - Rails 4 ได้อย่างไร

ฉันยังใหม่กับ Rails ดังนั้นความช่วยเหลือใด ๆ ที่มีให้จะได้รับการชื่นชมอย่างมาก

เป้าหมาย:

เมื่อผู้ใช้ทิ้งบันทึก (ความคิดเห็น) ไว้ในแบบฟอร์ม ฉันต้องการที่จะรู้ userr_id & form_id ในบันทึกนั้น ฉันไม่แน่ใจว่าจะเชื่อมโยง form_id กับบันทึกย่อได้อย่างไร เมื่อฉันสร้างบันทึก มันจะบอกฉันว่าผู้ใช้คนไหน (userr_id) เป็นคนสร้างบันทึก แต่ไม่ได้บอกฉันว่าบันทึกนั้นเป็นของรูปแบบใด (form_id) - ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมมาก - ขอบคุณ

ขณะนี้ฉันมีโมเดลและมุมมองด้านล่างที่สร้างขึ้น

  • [รุ่น] หมายเหตุเป็นของ: userj
  • [รุ่น] หมายเหตุเป็นของ: แบบฟอร์ม
  • [รุ่น] แบบฟอร์ม has_many :notes
  • [รุ่น] 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 (ในตารางบันทึกย่อ) ระบุว่าไม่มีในคอนโซล

<%= 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

form_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

person ARTLoe    schedule 09.07.2015    source แหล่งที่มา
comment
ในสถานที่ต่างๆ คุณมี userj และ userr มันคืออะไร? กรุณาแก้ไขรหัส/โพสต์   -  person steve klein    schedule 10.07.2015
comment
นอกจากนี้ ฉันคิดว่าคุณจำเป็นต้องเปลี่ยนบรรทัดแรกของ _form บางส่วนเป็น <%= simple_form_for(@form.note) do |f| %> หรือเชื่อมโยง @note เป็น @form ในคอนโทรลเลอร์ของคุณ ในบางส่วน คุณไม่ควรอ้างอิงถึง form_id หรือ params อย่างชัดเจน เพียงรวบรวมแอตทริบิวต์ที่คุณต้องการสำหรับอินสแตนซ์ของฟอร์มและสร้างในคอนโทรลเลอร์   -  person steve klein    schedule 10.07.2015
comment
:form_id ถูกส่งผ่านไปยังพารามิเตอร์ปกติหรือไม่ เราขอดูเส้นทางของคุณได้ไหม?   -  person Wes Foster    schedule 10.07.2015


คำตอบ (1)


โดยปกติแล้วคุณไม่ควรใช้พารามิเตอร์ในมุมมอง ในกรณีนี้ คุณสามารถ:

  1. สร้างตัวแปรอินสแตนซ์ @note ผ่านการเชื่อมโยงกับแบบฟอร์ม (@note = @form.notes.build) ใน FormsController#show และรวมฟิลด์ที่ซ่อนอยู่ในแบบฟอร์ม
  2. ส่งอินสแตนซ์ @form ไปยังบางส่วน เพื่อให้สามารถใช้ในรูปแบบ:

แบบฟอร์ม/show.html.erb:

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

บันทึก/_form.html.erb

    # ...
    <%= f.hidden_field :form_id, value: form.id %>
person mrodrigues    schedule 10.07.2015
comment
ขอบคุณ @mrodrigues มันได้ผล อะไรจะเป็นแนวทางปฏิบัติที่ดีที่สุดในการบรรลุเป้าหมายนี้ ฉันควรจัดทำบันทึกย่อเป็นทรัพยากรที่ซ้อนกันภายใต้แบบฟอร์มหรือไม่ - person ARTLoe; 10.07.2015