當我提交表單時,數據未在信息中發送並通過參數設置。我不能爲了我的生活找出原因。此表單位於計劃/展示操作中,因此您可以在此處設置@action變量。它通過JS發送。表單數據未通過POST參數發送
的routes.rb
resources :plans do
resources :actions
end
action.rb
belongs_to :plan
plan.rb
has_many :actions
plans_controller.rb
def show
@plan = current_user.plans.includes(:actions).find(params[:id])
@action = Action.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: @plan }
end
end
actions_controller.rb
before_filter :get_plan
def create
@action = @plan.actions.new(params[:action])
@action.user_id = current_user.id
@action.save
end
private
def get_plan
@plan = current_user.plans.find(params[:plan_id])
end
create.js.erb在視圖/行動夾
$('div#actions').prepend("<%= escape_javascript(render @action) %>");
$('div#<%= dom_id(@action) %>').effect('highlight');
_form.html.erb局部
通過POST發送<%= form_for ([@plan, @action]), remote: true do |f| %>
<%= f.text_field :desc %>
<%= f.number_field :days %>
<%= f.submit %>
<% end %>
參數(缺少行動哈希 - 爲什麼?)
Started POST "/plans/1/actions"
Parameters: {"utf8"=>"✓", "authenticity_token"=>"**removed**", "commit"=>"Create Action", "plan_id"=>"1"}
DB模式
create_table "plans", :force => true do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "actions", :force => true do |t|
t.string "desc"
t.integer "plan_id"
t.integer "days"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
生成的源代碼會很有趣。如果這是使用ajax(如create.js.erb)生成的,請在提交表單之前查看最新狀態。如果這只是特定瀏覽器中的一個問題(您測試了哪個版本),它可能會有所幫助 – 2012-02-20 14:26:31