ArgumentError,参数数量错误
我收到错误
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在使用 Devise + Omniauth 时遇到了类似的问题,症状包括:
结果是某种名称冲突,当我重命名我的方法和控制器时,这种冲突就消失了。例如:
Invites#process
错误(1 代表 0)邀请#process
-->错误(0 为 1)好友#添加
-->没有错误!希望这有帮助。
I had a similar issue when using Devise + Omniauth and symptoms included:
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.
尝试将
user_signed_in?
替换为current_user
。或者尝试在application_controller.rb
中添加before_filter :authenticate_user!
以避免控制器中出现 if 语句。Try to replace
user_signed_in?
withcurrent_user
. Or try to addbefore_filter :authenticate_user!
inapplication_controller.rb
to avoid if statement in your controller.我实际上发现了,我在routes.rb中缺少
resources :dplans
。感谢您的帮助!I actually figured it out, I was missing
resources :dplans
in routes.rb. Thanks for all your help!