views:

153

answers:

1

Hello,

I am having a bit of trouble with my rails application. It short, its a social networking app where users have a profile, have submissions, and can comment on submissions.

My routes are as follows:

map.connect '/:username', :controller => 'users', :action => 'show'  
map.connect '/:username/:id', :controller => 'submissions', :action => 'show'

So when they are viewing a submission the URL looks something like:

http://www.example.com/users%5Fusername/2342

The number is the id number of the category. So far so good. Where I am running into trouble is when I try to submit a comment on the submission. There is a form for submitting comments on each submission page that looks like this:

<% form_for Comment.new do |f| %>  
    <%= f.text_area :message %>  
    <%= f.submit "Submit", :disable_with => 'Submitting...' %>  
<% end %>

and the controller looks like this:

def create
    submission = Submission.find(params[:id])
    comment = cat.comments.create(params[:comment])
    comment.created_at = Time.now
    comment.save
    redirect_to submission
end

Now every time I try to make a comment submission Rails returns:

ActiveRecord::RecordNotFound in CommentsController#create Couldn't find Submission without an ID or undefined method `answers' for nil:NilClass

Basically rails isn't pulling the :id from the URL with params and I don't know why. The submission page displays correctly for each ID in the URL, so I don't get why its not pulling it for this form. If I name the id explicitly (IE Submission.find(2345)) it works perfectly...so what am I missing? Am I just being stupid?

My relationships are set up properly as well.

Thanks in advance.

+1  A: 

The following piece of code is generating a form that submits to comments controller:

<% form_for Comment.new do |f| %>

I believe you have to pass the the submission id then inside the form:

<%=hidden_field_tag(:submission_id, @submission.id)%>

And inside the comments controller you gotta grap that id:

def create
    submission = Submission.find(params[:submission_id])
    comment = submission.comments.create(params[:comment])
    comment.created_at = Time.now
    comment.save
    redirect_to submission
end

That should solve it, however I recommend doing nested resources as a second step.

khelll
that worked perfectly! Thanks so much! I just ran into the nested resources railscast earlier today and was planning on implementing it, but had yet to find anything about the hidden_field_tag you gave me, so thanks for that!
Ryan Max