在刷新页面之前,Param丢失或值为空

时间:2016-05-21 19:05:14

标签: ruby-on-rails params

大家好我真的很奇怪。我有活动和报名。每次当我想为特定事件登记时,Rails会告诉我:

  

错误:Param丢失或值为空:注册

当我刷新页面时,一切都很好。

new查看代码:

<%=form_for :enrollment do |f|%>
    <%= f.select(:car_id) do %>
        <% current_user.cars.each do |c| %>
            <%= content_tag(:option, c.mark, value: c.id) %>
        <% end %>
    <% end %>
    <%=f.submit :save %>
<%end%>

强烈的参数:

def enrollment_params
  params.require(:enrollment).permit(:car_id)
end

create方法:

def create
  @event=Event.find(params[:id])
  @enrollment = current_user.enrollments.build(enrollment_params)
  @enrollment.save
  @event.enrollments<<@enrollment
end

new行动:

 def new
    @enrollment=Enrollment.new
 end

记录:

Started POST "/events/39/enrollments/new" for 127.0.0.1 at 2016-05-21 22:53:52 +0300
Processing by EnrollmentsController#create as HTML
  Parameters: {"authenticity_token"=>"b4oZl8n8ALq1fgmqweygyWDF8oc7Ew3nelvhst5SRJkza8x92VNo3fXg+/gqw+NfKVPYvJfu9LCthRBGFB0xSA==", "id"=>"39"}
  Event Load (0.1ms)  SELECT  "events".* FROM "events" WHERE "events"."id" = ? LIMIT 1  [["id", 39]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 12]]
Completed 400 Bad Request in 5ms (ActiveRecord: 0.2ms)

路线:

          Prefix Verb   URI Pattern                                      Controller#Action
        new_user_session GET    /users/sign_in(.:format)                         devise/sessions#new
            user_session POST   /users/sign_in(.:format)                         devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                        devise/sessions#destroy
           user_password POST   /users/password(.:format)                        devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                    devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                   devise/passwords#edit
                         PATCH  /users/password(.:format)                        devise/passwords#update
                         PUT    /users/password(.:format)                        devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                          devise/registrations#cancel
       user_registration POST   /users(.:format)                                 devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                         devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                            devise/registrations#edit
                         PATCH  /users(.:format)                                 devise/registrations#update
                         PUT    /users(.:format)                                 devise/registrations#update
                         DELETE /users(.:format)                                 devise/registrations#destroy
               user_cars GET    /users/:user_id/cars(.:format)                   cars#index
                         POST   /users/:user_id/cars(.:format)                   cars#create
            new_user_car GET    /users/:user_id/cars/new(.:format)               cars#new
           edit_user_car GET    /users/:user_id/cars/:id/edit(.:format)          cars#edit
                user_car GET    /users/:user_id/cars/:id(.:format)               cars#show
                         PATCH  /users/:user_id/cars/:id(.:format)               cars#update
                         PUT    /users/:user_id/cars/:id(.:format)               cars#update
                         DELETE /users/:user_id/cars/:id(.:format)               cars#destroy
                   users GET    /users(.:format)                                 users#index
                         POST   /users(.:format)                                 users#create
                new_user GET    /users/new(.:format)                             users#new
               edit_user GET    /users/:id/edit(.:format)                        users#edit
                    user GET    /users/:id(.:format)                             users#show
                         PATCH  /users/:id(.:format)                             users#update
                         PUT    /users/:id(.:format)                             users#update
                         DELETE /users/:id(.:format)                             users#destroy
          event_comments GET    /events/:event_id/comments(.:format)             comments#index
                         POST   /events/:event_id/comments(.:format)             comments#create
       new_event_comment GET    /events/:event_id/comments/new(.:format)         comments#new
      edit_event_comment GET    /events/:event_id/comments/:id/edit(.:format)    comments#edit
           event_comment GET    /events/:event_id/comments/:id(.:format)         comments#show
                         PATCH  /events/:event_id/comments/:id(.:format)         comments#update
                         PUT    /events/:event_id/comments/:id(.:format)         comments#update
                         DELETE /events/:event_id/comments/:id(.:format)         comments#destroy
           event_results GET    /events/:event_id/results(.:format)              results#index
                         POST   /events/:event_id/results(.:format)              results#create
        new_event_result GET    /events/:event_id/results/new(.:format)          results#new
       edit_event_result GET    /events/:event_id/results/:id/edit(.:format)     results#edit
            event_result GET    /events/:event_id/results/:id(.:format)          results#show
                         PATCH  /events/:event_id/results/:id(.:format)          results#update
                         PUT    /events/:event_id/results/:id(.:format)          results#update
                         DELETE /events/:event_id/results/:id(.:format)          results#destroy
       event_enrollments GET    /events/:event_id/enrollments(.:format)          enrollments#index
                         POST   /events/:event_id/enrollments(.:format)          enrollments#create
    new_event_enrollment GET    /events/:event_id/enrollments/new(.:format)      enrollments#new
   edit_event_enrollment GET    /events/:event_id/enrollments/:id/edit(.:format) enrollments#edit
        event_enrollment GET    /events/:event_id/enrollments/:id(.:format)      enrollments#show
                         PATCH  /events/:event_id/enrollments/:id(.:format)      enrollments#update
                         PUT    /events/:event_id/enrollments/:id(.:format)      enrollments#update
                         DELETE /events/:event_id/enrollments/:id(.:format)      enrollments#destroy
                  events GET    /events(.:format)                                events#index
                         POST   /events(.:format)                                events#create
               new_event GET    /events/new(.:format)                            events#new
              edit_event GET    /events/:id/edit(.:format)                       events#edit
                   event GET    /events/:id(.:format)                            events#show
                         PATCH  /events/:id(.:format)                            events#update
                         PUT    /events/:id(.:format)                            events#update
                         DELETE /events/:id(.:format)                            events#destroy
                         GET    /attend/:id(.:format)                            events#attend
                         POST   /cancel/:id(.:format)                            events#cancel
                     all GET    /all(.:format)                                   users#all
                         POST   /all(.:format)                                   users#all
                myevents GET    /myevents(.:format)                              users#myevents
                         POST   /myevents(.:format)                              users#myevents
                         POST   /events/:id/comments/new(.:format)               comments#create
                         GET    /events/:id/comments(.:format)                   comments#index
                         PUT    /events/:id/comments/:id/edit(.:format)          comments#edit
                         POST   /events/:id/like(.:format)                       events#like
                         GET    /events/:id/like(.:format)                       events#like
                         POST   /events/:id/unlike(.:format)                     events#unlike
                         GET    /events/:id/unlike(.:format)                     events#unlike
                         POST   /users/:id/cars/new(.:format)                    cars#create
                         DELETE /events/:id/enrollments/:id(.:format)            enrollments#destroy
                         GET    /users/:id/usercars(.:format)                    users#usercars
                         GET    /users/:id/userevents(.:format)                  users#userevents
                         POST   /events/:id/results/new(.:format)                results#create
                         GET    /events/:id/statistics(.:format)                 results#statistics
                         GET    /events/:id/classfwd(.:format)                   results#classfwd
                         GET    /events/:id/classrwd(.:format)                   results#classrwd
                         GET    /events/:id/classawd(.:format)                   results#classawd
                         GET    /events/:id/enrollments(.:format)                enrollments#index
                         GET    /users/:id/mygallery(.:format)                   users#mygallery
                    root GET    /                                                welcome#Welcome

1 个答案:

答案 0 :(得分:0)

您的错误日志显示您在转到“/ events / 39 / enrollments / new”时尝试提交帖子。此路由需要是“获取”请求,否则它将尝试发布到服务器而不包含任何参数!进入你的config / routes文件并将其从'post'更改为'get'路由,这不应该发生。

编辑:您需要在表单中包含嵌套属性:

<%= form_for([@event, @enrollment]) do |f| %>

然后,您需要在新操作中定义@event,并在url中将params传递给它,就像在create action中一样。使用这些对象而不是:注册也应该指导它使用正确的操作(创建)而不是发布到新的。