ArgumentError,参数数量错误

发布于 2024-11-29 20:17:55 字数 1771 浏览 2 评论 0原文

我收到错误

ArgumentError in PagesController#home, wrong number of arguments (0 for 1)

,但我不知道为什么。我有一个来自 Devise 的 Users 模型和一个 Dplan 模型,其中 dplan belongs_to :user 和用户 has_many :dplans代码>.我正在尝试设置我的网站,以便您可以在主页上创建新的 dplan。我的主页视图是这样的:

<% if user_signed_in? %>
<h1>Hello <%= current_user.name %>! <h1>

<%= form_for @dplan do |f|%>
    <div class="field">
        <%= f.text_area :name %>
    </div>
    <div class="actions">
        <%= f.submit "Submit" %>
    </div>
<% end %>

<% else %>
<h1>DPlanner</h1>
<p>
    This is the home page for DPlanner.
</p>

<%= link_to "Sign up now!", new_user_registration_path, :class =>   "signup_button round" %>
<% end %>

这是 dplans_controller.rb:

class DplansController < ApplicationController

  def create
      @dplan = current_user.dplans.build(params[:dplan])
      if @dplan.save
          flash[:success]="Dplan created!"
          redirect_to root_path
      else
          render 'pages/home'
      end 
  end 

  def destroy
  end 

end

这是 Pages_controller.rb:

class PagesController < ApplicationController

    def home
        @title = "Home"
        @dplan = Dplan.new if user_signed_in?
    end

end

我不明白为什么会收到此错误消息 - 页面上需要的唯一参数是 dplan,我在页面中定义了它控制器。帮助!

这是 dplan.rb:

class Dplan < ActiveRecord::Base
    attr_accessible :name

    belongs_to :user

    validates :name, :presence=>true, :length => { maximum => 30 }
    validates :user_id, :presence =>true

end

I'm getting the error

ArgumentError in PagesController#home, wrong number of arguments (0 for 1)

but I don't know why. I have a Users model from Devise, and a Dplan model, where dplan belongs_to :user and a user has_many :dplans. I'm trying to set up my site so that you can create a new dplan on the home page. My home page view is this:

<% if user_signed_in? %>
<h1>Hello <%= current_user.name %>! <h1>

<%= form_for @dplan do |f|%>
    <div class="field">
        <%= f.text_area :name %>
    </div>
    <div class="actions">
        <%= f.submit "Submit" %>
    </div>
<% end %>

<% else %>
<h1>DPlanner</h1>
<p>
    This is the home page for DPlanner.
</p>

<%= link_to "Sign up now!", new_user_registration_path, :class =>   "signup_button round" %>
<% end %>

This is dplans_controller.rb:

class DplansController < ApplicationController

  def create
      @dplan = current_user.dplans.build(params[:dplan])
      if @dplan.save
          flash[:success]="Dplan created!"
          redirect_to root_path
      else
          render 'pages/home'
      end 
  end 

  def destroy
  end 

end

And here is pages_controller.rb:

class PagesController < ApplicationController

    def home
        @title = "Home"
        @dplan = Dplan.new if user_signed_in?
    end

end

I don't understand why I'm getting this error message - the only argument needed on the page is dplan, which I define in the pages controller. Help!

Here is dplan.rb:

class Dplan < ActiveRecord::Base
    attr_accessible :name

    belongs_to :user

    validates :name, :presence=>true, :length => { maximum => 30 }
    validates :user_id, :presence =>true

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老旧海报 2024-12-06 20:17:55

我在使用 Devise + Omniauth 时遇到了类似的问题,症状包括:

  1. 没有任何应用程序跟踪,
  2. 即使我的控制器方法为空,它也不会消失。
  3. 其他控制器工作得很好,

结果是某种名称冲突,当我重命名我的方法和控制器时,这种冲突就消失了。例如:

Invites#process 错误(1 代表 0)
邀请#process -->错误(0 为 1)
好友#添加 -->没有错误!

希望这有帮助。

I had a similar issue when using Devise + Omniauth and symptoms included:

  1. there was no application trace whatsoever
  2. it wouldn't go away even if my controller method was empty.
  3. other controllers worked just fine

it turned out to be some sort of name collision that went away when I renamed my method and controller. For example:

Invites#process Error (1 for 0)
Invitations#process --> Error (1 for 0)
Friends#Add --> No Error!

Hope this helps.

巷雨优美回忆 2024-12-06 20:17:55

尝试将 user_signed_in? 替换为 current_user。或者尝试在 application_controller.rb 中添加 before_filter :authenticate_user! 以避免控制器中出现 if 语句。

Try to replace user_signed_in? with current_user. Or try to add before_filter :authenticate_user! in application_controller.rb to avoid if statement in your controller.

尸血腥色 2024-12-06 20:17:55

我实际上发现了,我在routes.rb中缺少resources :dplans。感谢您的帮助!

I actually figured it out, I was missing resources :dplans in routes.rb. Thanks for all your help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文