I’m having problems implementing a kind of comments form, where comments (called “microposts”) belong_to both users and posts, users have_many comments, and posts (called “propositions”) have_many comments.
My code for the comments form is:
<%= form_for @micropost do |f| %>
<div class="field">
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
The MicropostsController has this in the create action:
def create
@proposition = Proposition.find(params[:proposition_id])
@micropost = current_user.microposts.build(params[:micropost])
@micropost.proposition = @proposition
if @micropost.save
flash[:success] = "Contribution submitted"
redirect_to root_path
else
@feed_items = []
render 'pages/home'
end
end
The form for creating a new micropost is on the same page as a proposition, yet the proposition id doesn’t seem to get passed at any point.
This is the error I get on submitting the micropost form:
ActiveRecord::RecordNotFound in
MicropostsController#createCouldn’t find Proposition without an
ID
Parameters are:
{“commit”=>”Submit”,
“micropost”=>{“proposition_id”=>””,
“content”=>”First comment”},
“authenticity_token”=>”TD6kZaHv3CPWM7xLzibEbaLJHI0Uw43H+pq88HLZFjc=”,
“utf8″=>”✓”}
I’m completely new to rails and very new to coding anything at all, so I’d be grateful for any help you can give me!
Thanks in advance.
Your params are:
So to get
proposition_id, you have to do :But this is empty. And there is nowhere else to get this
id, that’s why this line retrievesnil:Making this fail:
You must either:
add the
proposition_idas anhidden_fieldstore it in session
But I don’t know your context enough to give you the proper solution here.
EDIT:
In your link, replace:
with:
If it doesn’t work, show your params.
Note: it’s bad practice to rely on instance variables, you should send local variable to each partial