Wicked gem cannot find wedding ID in update action

只谈情不闲聊 提交于 2019-12-05 18:37:09

This line:

<%= simple_form_for(@wedding, :url => wizard_path, :method => :put, html: { class: 'form-horizontal'}) do |f| %>

Should be:

<%= simple_form_for(@wedding, :url => wizard_path(wedding_id: @wedding.id), :method => :put, html: { class: 'form-horizontal'}) do |f| %>

Note the wizard_path(wedding_id: @wedding.id) When you submit the form you should see parameters = {:wedding_id => some_number} in the logs.

Paste the output of the params for the update action if it doesn't work.

Edit:

You should have ":wedding_id" as part of the required url this will make it impossible to even generate a link to that controller unless it has a properly formatted url.

Replace this

resources :wedding_steps

with this

  scope "weddings/:wedding_id" do
    resources :wedding_steps
  end

So now a correct url would look like weddings/83/wedding_steps/weddingdetails. Likely one or more of your view helpers isn't including wedding_id properly and with this new constraint you will raise an error in the view, but this is a good thing since it will show you where the malformed link is.

I tried the solution provided by Schneems, however it is not running completely without errors. I implemented the following way.

Change

resources :wedding_steps

To

scope "weddings/:wedding_id" do
  resources :wedding_steps
end

The problem is parameters are displayed as forbidden based on the error that has been thrown as ActiveModel::ForbiddenAttributesError

To get rid off this,

Change

def update
  @wedding = Wedding.find(params[:wedding_id])
  @wedding.update_attributes(params[:wedding])
  render_wizard @wedding
end

To

def update
  @wedding = Wedding.find(params[:wedding_id])
  @wedding.update_attributes(wedding_params)
  render_wizard @wedding
end

private 
def wedding_params
  params.require(:wedding).permit(........your parameters here.................)
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!