0

这是我的设置:Rails 3.2.13

我已经为 new 传递了一个参数“restaurant_id”,现在我正在尝试通过提交按钮传递相同的参数以进行创建。但是,它似乎无法识别 create 的参数。

对于新的:

<%= link_to t('.new', :default => t("helpers.links.new")),
            new_reservation_path(:restaurant_id => restaurant.id),
            :class => 'btn btn-primary' %>

这在控制器中为 new 正确传递了 restaurant.id。然后,在新视图上,我有一个提交按钮:

对于创建:

<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
    reservations_path(:restaurant_id => @restaurant.id), :class => 'btn' %>

我在这里要做的是在创建和保存对象时再次传入 restaurant.id。但是,它没有出现在 create 方法中。@restaurant.id 有效地自行生成正确的 id,但不会通过。

这是我的控制器:

class ReservationsController < ApplicationController
    def new
        @reservation = Restaurant.find(params[:restaurant_id]).reservations.new
        @restaurant = Restaurant.find(params[:restaurant_id])
    end

    def create
        @reservation = Restaurant.find(params[:restaurant_id]).reservations.new(params[:reservation])

        if @reservation.save
          redirect_to root_url, notice: 'Reservation was successfully created.'
        else
          render action: "new", notice: 'There was an error.'
        end
    end
end

当它尝试查找餐厅时,我收到 No ID 错误:

ActiveRecord::RecordNotFound in ReservationsController#create

Couldn't find Restaurant without an ID

当我在控制器中测试 :restaurant_id 时,它返回空白。

这很奇怪,因为我使用完全相同的方法将 :restaurant_id 传递给我的新控制器,但是当我将它传递给我的创建控制器时它不起作用。

4

1 回答 1

0

I don't see the code for that submit button, but since that submit button sends a form, you need to pass the parameter as a hidden field(either hidden_field or hidden_field_tag. Arguments after the ? in the path in an HTML form's action simply won't get passed.

于 2013-07-07T21:43:12.247 回答