フォームを送信すると、データはポストで送信されず、パラメータ経由で設定されます。なぜ私は私の人生のために理解することはできません。このフォームはplan/showアクションに含まれていますので、ここで@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のような)で生成された場合、フォームを送信する前の最新の状態。これは特定のブラウザ(これはあなたがテストしたのですか?)の問題に過ぎない場合にも役立ちます。 –