gpt4 book ai didi

ruby-on-rails - Link_to create with parameter before - ForbiddenAttributesError 现在

转载 作者:太空宇宙 更新时间:2023-11-03 17:33:51 26 4
gpt4 key购买 nike

我想要什么:

我在 View 中需要一个按钮或链接(这无关紧要)到 Reservationcreate 操作 Controller 并给它一个参数。

并解决现在给我的 ForbiddenAttributesError。

这是我的模型和 Controller :

预订模型

class Reservation < ActiveRecord::Base
belongs_to :user
belongs_to :dinner
end

预订 Controller

class ReservationsController < ApplicationController
load_and_authorize_resource

def show
@reservations = Reservation.joins(:user).where('dinner_id' => params[:dinner_id]).select("users.*,reservations.*")
@dinnerid = params[:dinner_id]

respond_to do |format|
format.html
format.json { render :json => @reservations }
end
end

def create
@reservation = Reservation.new(params[:reservation])
@reservation.user_id = current_user.id

respond_to do |format|
if @reservation.save
format.html { redirect_to @reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: @reservation }
else
format.html { render :show }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end

# Never trust parameters from the scary internet, only allow the white list through.
def reservation_params
params.require(:reservation).permit(:dinner_id)
end

end

编辑:根据@Rahul Singh 的建议,这是我的实际代码,但有相对错误:

    <p id="notice"><%= notice %></p>

<table>
<thead>
<tr>
<th>Id</th>
<th>User id</th>
<th>Dinner id</th>
<th>User email</th>
<th>User name</th>
</tr>
</thead>

<tbody>
<% @reservations.each do |reservation| %>
<tr>
<td><%= reservation.id %></td>
<td><%= reservation.user_id %></td>
<td><%= reservation.dinner_id %></td>
<td><%= reservation.user.email %></td>
<td><%= reservation.user.name %></td>
</tr>
<% end %>
</tbody>
</table>

<br/>TRY 00a <br/>
<%= form_for(Reservation.new) do |f| %>
<%= f.hidden_field( :dinner_id, :value => @dinnerid.to_s) %>
<%= f.submit "Join1" %>
<% end %>
<br/> !!!!!!!!!!ERROR : ActiveModel::ForbiddenAttributesError

<br/>TRY 00b <br/>
<%= link_to "Join1", reservations_path(dinner_id:@dinnerid.to_s), method: :post %>
<br/> !!!!!!!!!!ERROR : param is missing or the value is empty: reservation

我提供错误的截图:

表格错误:https://www.dropbox.com/s/i2x1m520ptqdj56/createReservationForm.jpg

link_to 错误:https://www.dropbox.com/s/8xjwee5oo7q6uhk/createReservationLink_to.jpg

最佳答案

这应该可行

<%= form_for(Reservation.new) do |f| %>
<%= f.hidden_field( :dinner_id, :value => @dinnerid.to_s) %>
<%= f.submit "Join1" %>
<% end %>

单击 Join1 按钮会将表单提交给 ReservationsController 创建操作。

并通过链接试试这个

<%= link_to "Join1", reservations_path(dinner_id:@dinnerid.to_s), method: :post %>

要使上述工作正常,请在您的 routes.rb 中添加以下内容

resources :reservations

关于ruby-on-rails - Link_to create with parameter before - ForbiddenAttributesError 现在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24190597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com