更新额外params记录

问题描述 投票:0回答:2

我目前正在对我的新的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 descriptionwebsite插入我的预订表,但事实并非如此。

我得到了这个错误了几次:

ActiveRecord::RecordNotFound (Couldn't find Reservation with 'id'={"id"=>"23"}):

ruby-on-rails
2个回答
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

0
投票

我相信你需要在你的预约控制器的最新动作。您创建行动,创建了保留意见,但是,如果接下来的形式要求更多的信息,所以对我来说这是一个更新...

你可以写这样的事情

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
© www.soinside.com 2019 - 2024. All rights reserved.