Saya baru mengenal Rails jadi bantuan apa pun yang ditawarkan akan sangat dihargai.
Tujuan:
ketika pengguna meninggalkan catatan (komentar) pada formulir, saya ingin mengetahui userr_id & form_id pada catatan itu. Saya tidak yakin bagaimana cara menautkan form_id ke catatan. ketika saya membuat catatan, ia memberi tahu saya pengguna mana (userr_id) yang membuatnya tetapi tidak memberi tahu saya bentuk (form_id) mana yang dimiliki catatan itu - bantuan apa pun akan sangat kami hargai - terima kasih
Saat ini saya telah membuat model dan tampilan di bawah ini
- [model] catatan milik_to :userj
- [model] catatan milik_to :form
- [model] formulir has_many :catatan
- [model] penggunaj has_many :catatan
konsol saya:
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
}
]
model:
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
formulir/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>
catatan/_form.html.erb
Saya mencoba meneruskan form_id seperti di bawah ini tetapi ketika saya membuat catatan untuk suatu formulir, itu tidak memberi tahu saya formulir mana yang menjadi milik catatan itu karena kolom form_id (di tabel catatan) menyatakan nihil di konsol
<%= 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 %>
catatan_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
userj
danuserr
. Yang mana? Harap perbaiki kode/posting. - person steve klein   schedule 10.07.2015<%= simple_form_for(@form.note) do |f| %>
atau mengaitkan @note ke @form di pengontrol Anda. Secara parsial, Anda juga tidak boleh merujuk secara eksplisit keform_id
atauparams
, cukup kumpulkan atribut yang Anda perlukan untuk instance formulir dan buat di pengontrol. - person steve klein   schedule 10.07.2015:form_id
diteruskan ke params baik-baik saja? Bisakah kami melihat rute Anda? - person Wes Foster   schedule 10.07.2015