创建后如何修复缺少的参数

时间:2019-05-04 07:11:02

标签: ruby-on-rails

我正在订阅应用程序,但是我一直在与产品的展示/页面上创建订阅进行堆叠。由于错误

  

ActionController :: ParameterMissing(参数缺失或值为空:预订):

routes.rb

resources :products do
  resources :subscriptions, only: [:create]
end

products / show.html.erb

  <div class="subscribe">
    <%= form_with(model: @subscription, url: product_subscriptions_path(product_id: @product.id)) do |f| %>
      <div>
        <%= f.text_field :email %>
        <%= f.submit "Register", class: "btn"%>
      </div>
    <% end %>
  </div>

products_controller.rb

def show
  @product = Product.find_by_id(params[:id])
  @subscription = Subscription.new
end

subscription_controller.rb

def create
  @product = Product.find(params[:product_id])
  @subscription = @product.subscriptions.build(create_params)
  @subscription.save
end

def create_params
  params.require(:subscription).permit(:email, :product_id)
end

1 个答案:

答案 0 :(得分:0)

此处的url帮助器需要两个参数,因为它是nested route.

url: product_subscriptions_path(product: @product, subscription: @subscription))