我目前正在开发新的Web应用程序。我的客户可以预订,之后他们得到重定向到一个表单,在那里他们可以提交其他信息,如:描述,网站,电子邮件,密码...
在记录已经与create
方法创建,一个成功的付款后客户是重定向到我的submit
方法。我已经尝试过@reservation.update(reservation_params)
,但这没用。
我的路线:
resources :reservations, only: [:approve, :decline, :submit] do
member do
match 'submit', via: [:get, :post]
end
end
我的预定控制器:
class ReservationsController < ApplicationController
before_action :authenticate_user!
before_action :is_admin, only: [:approve, :decline, :your_reservations]
before_action :set_reservation, only: [:approve, :decline, :submit]
def create
service = Service.find(params[:service_id])
if current_user.admin?
flash[:alert] = "Du kannst nicht dein eigenes Angebot kaufen"
elsif current_user.stripe_id.blank?
flash[:alert] = "Füge eine Zahlungsmehtode hinzu"
return redirect_to payment_method_path
else
@reservation = current_user.reservations.build
@reservation.service = service
@reservation.price = service.price
if @reservation.Bearbeitung!
flash[:notice] = "Ihre Anfrage wurde erfolgreich versendet"
ReservationMailer.confirm_email_to_guest(@reservation.user, service).deliver
confirm_sms(service, @reservation)
else
charge(service, @reservation)
end
end
redirect_to submit_reservation_path(@reservation)
end
def submit
redirect_to root_path, alert: "Du hast keine Berechtigung!" unless current_user.id == @reservation.user_id
end
def set_reservation
@reservation = Reservation.find(params[:id])
end
end
我submit.html.erb:
<%= form_for submit_reservation_path, method: :post do |f| %>
<div class="form-group">
<label>Aufgabe</label>
<%= f.text_field :description, placeholder: "Aufgabe", required: true, class: "form-control" %>
</div>
<div class="text-center"><%= f.submit "Veröffentlichen", class: "btn btn-primary btn-block" %></div>
<% end %>
我删除了params.permit(:description)
,因为它不起作用。我预期额外PARAMS description
,website
被插入到我的保留表,但情况并非如此。
我几次遇到此错误:
ActiveRecord::RecordNotFound (Couldn't find Reservation with 'id'={"id"=>"23"}):
答案 0 :(得分:1)
此不引用要预约对象:
form_for submit_reservation_path, method: :post do |f|
像这样更改它:
form_for @reservation, url: submit_reservation_path(@reservation), method: :post do |f|
现在的URL将被精细而id PARAM应该是正确的。
然后,在你提交的动作,你可以更新记录@reservation.update(reservation_params)
就个人而言,只有两个技巧,我会将“如果不是当前用户,则重定向”行移至before_action以清除该行,并对get和post方法使用不同的操作,如下所示:
resources :reservations, only: [:approve, :decline, :submit] do
member do
get :submit
post :submit, action: :do_submit
end
end
#controller
before_action :set_reservation, only: [:approve, :decline, :submit, :do_submit]
before_action :check_reservation_ownership, only: [:submit, :do_submit]
def submit
end
def do_submit
@reservation.update_attributes(reservation_params)
render action: :submit #or redirect, or something else
end
private
def check_reservation_ownership
redirect_to root_path, alert: "Du hast keine Berechtigung!" unless current_user.id == @reservation.user_id
end
答案 1 :(得分:0)
我相信您需要在预订控制器中执行更新操作。 您的create动作会创建预订,但是如果下一个表单要求提供更多信息,对我来说这是一个更新...
您可以这样写
def update
if @reservation.update_attributes(reservation_params)
redirect_to your_path
else
render :new
end
end
private
def reservation_params
params.require(:reservation).permit( all your attributes including, :description, :website)
end