I have a form_for a comment. Comments belongs_to Issue. I have a comments form on my issue#show view, which is where my form_for is. I create the comment object @comment in my issue#show controller action. When i submit the form, it calls the comment#create controller action. However, I need a variable called :issue_id. I have issue_id in the issue#show view, and I can even set the @comment object to have issue_id. However, when it gets passed to the comments#create controller, the issue_id isn’t being passed in the params[:comment] so when the comment gets created and saved, it’s not there. Here are my files:
issue#show view:
<%= form_for @comment do |f| %>
<%= render 'comment_fields', :f => f %>
<%= f.submit "Submit" %>
<% end %>
issue#show controller:
def show
@issue = Issue.find(params[:id])
@votes = Votership.where(:issue_id => @issue.id)
@current_user_vote = @votes.where(:user_id => current_user.id).first
@comment = Comment.new(:issue_id => @issue.id)
end
and the comments#create controller where the comment gets created:
def create
@comment = Comment.new(params[:comment])
@comment.user = current_user
@comment.save
redirect_to :back
end
I tried adding a hidden field in the form, but it kept creating a hash of values and the key would be the issue_id but I couldn’t figure out how to set the value of the hidden field, which is weird. How do I do this correctly?
PS comments belong_to issues. issues has many comments. issues belongs_to apps. apps has many issues.
I do not actually understand your complain but from what i understand you are trying to get the issue id to be saved with the comment. you should have a hidden field in your form like
if this is not the solution you need pls let me know so i can help again.