How to assign the value in ruby commands in file with extension html.erb for the following scenario: Comment.request_id = id (of Request) Note Comment and Request both are model
move code into the controller or model
Kishoj B. wrote in post #959256:
How to assign the value in ruby commands in file with extension html.erb for the following scenario: Comment.request_id = id (of Request) Note Comment and Request both are model
There's so much wrong with what I see here I don't know where to start. You really need to study the Model-View-Controller (MVC) design pattern. Otherwise you'll never get Rails to work for you as it was designed to do.
But, here are the top five things I see wrong:
5. Request is probably a bad name for a model class in web applications. It's could be too easily confused with the actual HTTP request object.
4. The request_id appears to be an instance method, but you're showing it called on the Comment class.
3. Don't manipulate primary and foreign keys directly. Manage associations through association manipulation methods.
@comment = @request.comments.build(:title => "Learn MVC", :author => "Robert")
@request.comments << an_existing_comment
Note: What you see here is controller code not view code.
2. Fetching data IS NOT the responsibility of the view.
RequestController (request_controller.rb)
Thank you all for your warm response!!! I got the solution. Its something like below
<% form_for [@request, Comment.new] do |f| %> <p> <%= f.hidden_field :request_id, :value => @request.id %> ................................... </p> ................................................................................. <% end %>
You can avoid the hidden field if you setup a nested route for request/ ##/comments.