简体   繁体   中英

Rails Error: Can't understand the message - ArgumentError: wrong number of arguments (0 for 1)

I am trying to make an app with Rails 4.

I have three models being:

User.rb (for authentication and fixed details)
Profile.rb (for user details which can change)
project.rb (for projects that users create)

In my projects_controller, I have:

def show

    @project = Project.find(params[:id])
    @creator = User.find(@project.creator_id)
    @creator_profile = @creator.profile

  end

The association between user and project is that project belongs to user. In my project table, I have an integer attribute called 'creator_id'. I don't have a separate 'user_id' field.

I have creator, because I want to define users who create projects (so that the project creator links to that user's profile (as opposed to the profile of the current_user who is looking at the project).

In my project model, I have defined a scope and a method as follows:

 scope :creator, lambda { where(@creator_profile = user.profile_id)}

 scope :current, lambda { where('project.start_date >= ?', Date.today)}

 def self.all_current_for_creator(creator)
    if creator.profile.present?
       creator.current
    else
      guest_visible
    end
  end 

Then, in my profile show, I want to show all of the creator's current projects. To do this I have:

 <div class="row">
      <div class="col-md-10 col-md-offset-1">
      <% Project.all_current_for_creator.sort_by(&:created_at) do |project| %>
      <div class="row">
            <div class="col-md-4">
              <div class="indexdisplay">
                <%= image_tag project.hero_image_url, width: '100%', height: '200px' if project.hero_image.present? %>
              </div>
            </div>
            <div class="col-md-8">
              <div class="row">
                <div class="col-md-12">
                  <div class="indexheading"> <%= link_to project.title, project %> </div>
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <div class="indexsubtext">    
                    <%= truncate(project.description, :ommission => "...", :length => 250) %>
                  </div>              
                </div>
              </div>
            </div>
      </div>

     <% end %>

When I try this, I get an error that says:

ArgumentError at /profiles/3
wrong number of arguments (0 for 1)

I can't understand what this error means. I'm not sure what is looking for an argument and I can't see anything that doesn't have an argument & looks like it should.

Can anyone see where I've gone wrong?

You are calling:

Project.all_current_for_creator

Without arguments. You should call it like:

Project.all_current_for_creator(@creator)

Update

.sort_by(&:created_at) do |project|

Should be:

.sort_by(&:created_at).each do |project|

Because in the first case you are passing two blocks &:created_at and do ... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM