Frage

I have a strange problem. I've been coding in Rails for, off and on, a year. I created a new project recently and used scaffolding. Things were going fine, yesturday I started implementing some favoriting features. Now I have a strange problem. I rolled back the stuff I did last night but still have the problem. First

 Entry belongs to user
 User has many entries

My Entry show method in my controller is very standard and simple

def show
  @user = User.find(params[:user_id])
  @entry = @user.entries.find(params[:id])

  respond_to do |format|
   format.html # show.html.erb
   format.json { render :json => @entry }
  end
end

When I view the entry from a normal link in the entries index

<%= link_to 'Show', user_entry_path(@user, entry) %>

I takes me to where it should go:

/users/4/entries/11

When I create new things still look good

/users/4/entries/new

Until I click "create entry" or the submit button

<div class="actions">
  <%= f.submit %>
</div>

Then it goes to

/entries/20  ...with the error:
ActiveRecord::RecordNotFound in EntriesController#show
Couldn't find User without an ID

If I go back to the entries index however, the file new entry is there and the show link takes me to the right place. Thoughts? Your help is appreciated!

War es hilfreich?

Lösung

The error message tells you that User.find(params[:user_id]) couldn't find a user with that ID. Try checking the structure of the GET parameters in the server logs.

If your GET path is /entries/20, then the path only has an entry ID and is missing a user ID. You might be able to fix this in your Controller#create by having it redirect to user_entry_path instead of entry_path.

Andere Tipps

How does your form look like? I think you have nested routes? Your form should look like following:

<%= form_for [@user, @entry] do |f| %>
  <% # your fields %>
<% end %>

Your form seems to point to resources entry, instead of the nested ressource..

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top