How can i bring 2 variables into the view. I am newbie in Ruby on rails.
What will the sytax to bring 2 or more values into a view.
<%= form_for(#user) do |f| %>
EDIT :
<%= form_for(#user) do |f| %>
prohibited this user from being saved: </h2>
<u1>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</u1>
</div>
<% end %>
<%= debug #user %>
<div class = "field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
Let's say that in the above code I want to print values from 2 objects and also submit them. How can I do that ?
Well, if you need to use 2 variables, I think it would be better to use two separate forms because they are unrelated. If two variables have relations to each other, you should use accepts_nested_attributes_for and fields_for to do work.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
from your above comment "I am talking about loading 2 model into one form" I think you need to use two model in one form so rails cast produce good episode on using nested model for form .
You can use accepts_nested_attributes_for for eg. for survey is one model and question is another model you can use question as nested_attributes in survey and same you can use answers model in survey model in same form.
For more you can read following link.
http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast
I hope this will help you.
Thanks.
Related
I have a #friend model that has_and_belongs_to_many #interests and vice versa. Each interest has a name:string. How do I show all the interests by their name next to each friend?
I tried
friend.interests.count
which shows the correct number, but for
friend.interests.first
the result is
#<Interest:0x00007f959e103250>
How do I display the name of this interest from the database in a view?
<%= friend.interests.count %>
<%= friend.interests.first %>
You can get the name of the interest, just by accessing the friend.interests.first.name. And for listing all the interests you can iterate and show the name of them.
<% friend.interests.each do |interest| %>
<%= interest.name %>
<% end %>
Just put the attribute after the object:
<%= friend.interests.first.name %>
Hello I am trying to create a form in which Model A has many and belong to itself. I tried using nested_form and no luck yet.
I want to create nested form for just Model A.
Can anyone suggest me correct path ?
Without more details on your models and structure it's hard to help, maybe include some code too?
For nested models you typically do something like this:
<%= form_for #model_a do |model_a_form| %>
<%= model_a_form.text_field :attribute_1 %>
<%= model_a_form.text_field :attribute_2 %>
<%= fields_for :child_models, #model_a.child_models do |child_models_fields| %>
<%= child_models_fields.text_field :attribute_1 %>
<%= child_models_fields.text_field :attribute_2 %>
<% end %>
<%= model_a_form.submit %>
<% end %>
See https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for for more info.
Is possible to utilize the habtm checkboxes on create action?
because this:
<%= hidden_field_tag "product[size_ids][]", nil %>
<% Size.order(:size).each do |size| %>
<li> <%= check_box_tag "product[size_ids][]", size.id, Product.size_ids.include?(size.id), id: dom_id(size) %>
<%= label_tag dom_id(size), size.size %>
</li>
<% end %>
was on update and was working since was brought to create page rails spits out the
undefined method `size_ids' for #
so, have a way to utilize the habtm on a create action?
Since you're likely dealing with one item, you probably mean:
#product.sizes_ids
The Product model doesn't have a direct association with any sizes, it's only instances of it that do.
use collection check boxes getting all of the model with ids exemple:
<%=p.collection_check_boxes :size_ids, Size.all, :id, :size %>
I am building a very simple movie review app with Rails, which does not have any authentication system.
The app has:
a User model (id, name, email), which has many Reviews and has many Comments
a Review model (id, title, image, content), which belongs to one User and has many Comments
a Comment model (id, content), which belongs to one User and belongs to one Review
Here is the _form.html.erb file for comments:
<%= bootstrap_form_for(#comment) do |f| %>
<% if #comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_field :content %>
</div>
<div class="field">
<%= f.number_field :review_id %>
</div>
<div class="field">
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
When adding/editing a comment, the user can chose the Review to which the comment will be attributed, thanks to:
<div class="field">
<%= f.number_field :review_id %>
</div>
which lets him chose between review ids.
Instead, I would like the user to be able to select the review title of the review he wants to comment upon.
I tried to modify the review model with a to_param method, but it did not solve the problem and actually created some other bugs in the app.
How can I solve the problem?
Further to ply's answer, what you have to remember is when you populate an object-based form, you're really taking a Model's attributes & populating them
form_for:
Typically, a form designed to create or update a resource reflects the
identity of the resource in several ways: (i) the url that the form is
sent to (the form element's action attribute) should result in a
request being routed to the appropriate controller action (with the
appropriate :id parameter in the case of an existing resource), (ii)
input fields should be named in such a way that in the controller
their values appear in the appropriate places within the params hash,
and (iii) for an existing record, when the form is initially
displayed, input fields corresponding to attributes of the resource
should show the current values of those attributes.
--
You are populating the Comment model object - this will have attributes defined in your database, such as body, title etc
One of the attributes in the Comment model is the review_id foreign_key
To the Comment model, it does not matter how review_id is passed to it; just that it's done. This is why it does not matter if you use a text_field to input the id directly, or if you use a select tag to help the user select the item they want
--
collection_select
<%= f.collection_select(:review_id, Review.all,
:id, :title,
{:prompt => 'Please select the review of this comment'}) %>
This will give you a select box where you can pick the review
--
Nested Route
A much better way to do this is to use a nested route, so you can set review_id from the parmas:
#config/routes.rb
resources :reviews do
resources :comments #-> /reviews/1/comments/new
end
#app/controllers/comments_controller.rb
def create
#comment = Comment.new(comment_params)
#comment.save
end
private
def comment_params
params.require(:comment).permit(:content).merge(review_id: params[:review_id])
end
Not sure if I follow, but could you just use a select tag here?
This assumes you have an instance variable named #reviews defined in your controller that will be available.
In this case #reviews could be something like Review.all
select_tag "review", options_from_collection_for_select(#reviews, "id", "title"), prompt: "Select a review"
There is 'FoodType' model which are describes types of food in restaurants. I need to make view for creating a new restaurant, and I need to have list of checkboxes in order to allow user to setup types of food for each restaurant. I want to have something like this:
<% FoodType.all.each do |food_type| %>
...
<div class="row">
<%= f.check_box :food_types[0] %>
</div>
...
<% end %>
I want to have parameters like params[restaurant][food_types][0] = true in order to make some actions after creating. Please, tell me, how can I do it? Thanks in advance.
Presumably you have a join table which joins restaurants and food types? Let's say that you have one called restaurant_food_types (with a model RestaurantFoodType), which has restaurant_id and food_type_id?
You will then have this association in restaurants:
Restaurant < ActiveRecord::Base
has_many :restaurant_food_types
has_many :food_types, :through => :restaurant_food_types
This will give you the method .food_type_ids which you can call on a restaurant to set the joins. It's this method that you should hook into in your form: it expects an array of ids, so you need to set up an array-style parameter (one where the name ends in []) You may need to use check_box_tag rather than .check_box, to access an array-style parameter name: i would do this:
<% form_for #restaurant do |f| %>
<% FoodType.all.each do |food_type| %>
...
<div class="row">
<%= check_box_tag "restaurant[food_type_ids][]", food_type.id, #restaurant.food_type_ids.include?(food_type.id) %><%= food_type.name %>
</div>
...
<% end %>
<%= f.submit "Save" %>
<% end %>
Like i say i'm using a check_box_tag here but there might be a nicer way to hook into the food_type_ids method.