1

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
4

3 回答 3

2

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

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

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

Invites#process错误(1 代表 0)
Invitations#process--> 错误(1 代表 0)
Friends#Add--> 无错误!

希望这可以帮助。

于 2011-08-18T09:35:05.483 回答
0

我实际上想通了,我resources :dplans在 routes.rb 中丢失了。感谢你的帮助!

于 2011-08-18T17:22:10.873 回答
0

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

于 2011-08-17T03:43:57.703 回答