Assign value in ruby in html.erb file?

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

On 10-11-04 04:12 AM, kishoj wrote:

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


Kind Regards,
Rajinder Y. | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1

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:

  1. 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.

  2. 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.

  1. Fetching data IS NOT the responsibility of the view.

RequestController (request_controller.rb)

@my_request = Request.find(params[:id])

  1. Manipulation of data IS NOT the responsibility of the view.

You need a CommentsController class that is used to fetch data from the
Comment model and provide that data to one of the views. User input is
provided to the controller though the params hash and the model objects
are provided to the view through instance variables in the controller.
These get pushed (or copied) into the view. This means that the
“Comment” and/or “Request” model objects would be available to the view
as @comment or @request (or whatever names to give to the instance
variables).

On Nov 5, 3:25pm, kishoj [email protected] wrote:

Thank you all for your warm response!!!
I got the solution. Its something like below

<% form_for [@request, Comment.new] do |f| %>


<%= f.hidden_field :request_id, :value => @request.id %>

<% end %>

You can avoid the hidden field if you setup a nested route for request/
##/comments.

http://guides.rubyonrails.org/routing.html#nested-resources

Thank you all for your warm response!!!
I got the solution. Its something like below

<% form_for [@request, Comment.new] do |f| %>


<%= f.hidden_field :request_id, :value => @request.id %>



<% end %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs