【问题标题】:Devise routing error when i put the before_action :authenticate_user当我放置 before_action 时设计路由错误:authenticate_user
【发布时间】:2015-06-02 02:42:09
【问题描述】:

我有一个会话列表(不是设计会话,是电影会话),并且我有一个指向此会话的新报价的链接,报价是来自用户的嵌套资源。要购买(创建报价),必须登录用户(因为创建报价需要用户的参考)。当我把 before_action :authenticate_user!在 session_controller 中(再次是电影会话而不是设计会话)我收到此错误:

NoMethodError in Devise::Sessions#new
 Showing /home/ubuntu/workspace/app/views/users/sessions/new.html.erb where line #10 raised:

undefined method `name' for #<ActionView::Helpers::FormBuilder:0x000000034bc708>

这是我的路线.rb:

 Rails.application.routes.draw do
  resources :tickets
  resources :offers
  resources :prices
  resources :movies
  devise_for :sessions, controllers: {
   new: 'session/new'
  }
  resources :sessions do
    resources :offers
  end
  resources :rooms
  resources :seats
  resources :cinemas
    get 'welcome/index'
  devise_for :users, controllers: {
     registrations: 'users/registrations'
  }
  root 'welcome#index'
 end

这是我的 session_controller(电影会话,不是设计):

class SessionsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_session, only: [:show, :edit, :update, :destroy]
  respond_to :html
  ...

new.html.erb (session_controller):

 <h1>New session</h1>
 <%= render 'form' %>
 <%= link_to 'Back', sessions_path %>

_form.erb(会话):

<%= form_for(@session) do |f| %>
  <% if @session.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@session.errors.count, "error") %> prohibited this session from being saved:</h2>

      <ul>
      <% @session.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :cinema_id %><br>
    <%= f.collection_select :cinema_id, Cinema.all, :id, :nameCine %>
  </div>

  <div class="field">
    <%= f.label :room_id %><br>
    <%= f.collection_select :room_id, Room.all, :id, :description %>
  </div>

  <div class="field">
    <%= f.label :movie_id %><br>
    <%= f.collection_select :movie_id, Movie.all, :id, :title %>
  </div>

  <div class="field">
    <%= f.label :bgn_date %><br>
    <%= f.datetime_select :bgn_date %>
  </div>
  <div class="field">
    <%= f.label :end_date %><br>
    <%= f.datetime_select :end_date %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

【问题讨论】:

  • 您认为问题(不在路由器中)。查看错误信息。
  • 是的,但没有 before_action :authenticate_user!一切正常
  • 显示您的new.html.erb 文件。
  • 编辑:添加 new.html.erb
  • 没有第 10 行出现错误。

标签: ruby-on-rails-4 devise


【解决方案1】:

这是新手错误,我一直在寻找设计注册问题的任何内容。但是错误出现在(设计)会话/新视图中,我已经自动生成了名称字段,但是不使用登录,只有电子邮件和传递字段。 我从(设计)会话中删除了名称字段/新的一切都像魅力一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多