I'm trying to use multiple instance of the same class in a view with rails.
Basically i need to show a Branch with all its attributes and in the same page i have a form_for that need an empty Branch object. The problem is that when i create the empty Branch instance in the controller "#newBranch" the view can't access the first one anymore
here what I do in the controller:
def show
#customer = Customer.find(params[:id])
#branches = #customer.branches
#newBranch = #customer.branches.new #this is for the form_for
#newContact = #newBranch.build_contact #this is for the fields_for
end
if i try to use a singular item of the collection #branches for example:
<div class = "branch_container">
<%= render :partial => "customers/branch", :collection => #branches %>
</div>
and then inside the branch partial:
<%= branch.contact.name %>
i have the message:
"undefined method `name' for nil:NilClass"
All the models associations work fine and if i don't instantiate #newBranch and #newContact the problem disappear.
Basically i need to use two instance of the same Class (for example "#branches" in one partial and "#newBranch" in another one) in the same view.
What could be the solution?
Thank you.
Provide those instances as local vars:
<%= render :partial => "customers/branch", :collection => #branches, :locals => {:branch => #branch, :customer => #customer} %>
Oops didn't read well. For a new branch, contact is not set, so nil. Just check for this situation.
<% if branch.contact %>
_Your code_
<% else %>
No contact assigned
<% end %>
At the end i created #newBranch and #newContact in the view inside the form in the following way:
<%= form_for(newBranch = Building.new, :html => { :multipart => true }) do |building_form| %>
<% newBranch.build_contact %>
etc... etc...
Related
In a rails 4 application, I have a book resource, that is a Book model with its controller, views and route. It's what gets created by:
rails g scaffold book title
Now I want to have another set of views (and another controller) that allows to manage the same model, maybe dedicated to a different user.
I want both the creating function and the editing function to be available on this different route and view, .
Let's call it book2.
The views in the /book2 url should operate on the Book2sController.
form_for support
But the form_for guesses the submit route (and puts it in the action attribute) from the model class, that, being it always Book, lets rails guess that the submit url is /books/1 for edit or /books/ for new and not /book2s/1 for edit and /book2s/ for new as it should be.
So i found this solution, but i find it to be a bit cumbersome.
Is there anything better out there?
<%= form_for #book, :url => #book.new_record? ? url_for(book2s_path) : url_for(book2_path(#book)) do |f| %>
<%= f.text_field :title %>
<% end %>
You could set the url in your controller.
def new
# ...
#form_url = book2s_path
# ...
end
def edit
# ...
#form_url = book2_path(#book)
# ...
end
Then your view becomes:
<%= form_for #book, :url => #form_url do |f| %>
<%= f.text_field :title %>
<% end %>
I have also seen:
<%= form_for #book, :url => {:controller => 'book2s', :action => #action} do |f| %>
<%= f.text_field :title %>
<% end %>
and you just set #action in the controller (probably create or update).
Note that you don't need to include the url_for like you have.
I am rendering a partial like this:
$("#box_container").html("<%= escape_javascript( render :partial => 'contacts/contact_tile', :collection => #contacts) %>")
Problem is that my partial is expecting the variable 'contact'.
ActionView::Template::Error (undefined local variable or method `contact'
I simply want to tell the partial to expect a variable contact. Should iterate through #contacts as contact. How do I do that?
Found this is also helpful from the docs. You aren't limited to having the variable named after the partial:
http://guides.rubyonrails.org/layouts_and_rendering.html
To use a custom local variable name within the partial, specify the
:as option in the call to the partial:
<%= render :partial => "product", :collection => #products, :as => :item %>
With this change, you can access an instance of the #products collection as the item local variable within the partial."
The documentation at http://guides.rubyonrails.org/layouts_and_rendering.html says:
When a partial is called with a pluralized collection, then the
individual instances of the partial have access to the member of the
collection being rendered via a variable named after the partial.
So it will be passed a variable called "contact_tile" instead of "contact". Perhaps you can just rename your partial.
If this naming is important, you could do it explicitly without the collection option by something like:
#contacts.each { |contact| render :partial => 'contacts/contact_tile', :locals => {:contact => contact } }
(although as a commenter pointed out, this may not be as performant)
Latest syntax are :
index.html.erb
<%= render partial: "product", collection: #products %>
_product.html.erb
<p>Product Name: <%= product.name %></p>
#products is used in partial as product
Where #products can be considered as Product.all
and product can be considered as a row of product i.e. Product.first as looped all product one by one.
You can specify a custom variable name as the default with the keyword as:
<%= render partial: 'line_items/line_item', collection: order.line_items, as: :item %>
I have list dropdown list in my activeadmin that populates the recipe and menu. I'm trying to override the create method but it is not working
<%= semantic_form_for [:admin, #menu_recipe] do |f| %>
<p>
<%= f.collection_select :recipe_id,
Recipe.all,:id,:name,:prompt => true%>
</p>
<p>
<%= f.collection_select :menu_id,
Menu.all,:id,:name,:prompt => true%>
</p>
<%= f.buttons :commit %>
<%end%>
Whenever I try to catch the and create or group it, it returns with a Couldn't find Recipe without an ID error
my active admin controller which i override is
ActiveAdmin.register MenuRecipe do
menu :parent => "Manage Package"
form :partial => "menu_recipe"
controller do
def new
new! do |format|
#menu_recipe = MenuRecipe.new
end
end
def create
create! do |format|
recipe = Recipe.find(params[:recipe_id])
menu = Menu.find(params[:menu_id])
#menu_recipe = #menu.add_recipe(menu.id)
if #menu_recipe.save
redirect_to {admin_menu_recipe_url}
end
end
end
end
end
im i doing it right? if anything is needed please just ask thanks in advance
My guess is it's how you are getting the recipe_id. I would maybe debug the params and see what the actual values are.
You may need to do something like this:
params[:menu_recipe][recipe_id]
new on rails, i am having problem in passing select_tag value(in the view file) to controller.
my view controller file is like
class ProjectStatusController < ApplicationController
def index
#projects = Project.find(:all, :select => "name")
end
def show
lookup = params[:project]
#rows = Project.find_by_lookup(lookup)
end
end
and view file is like
<% form_tag("project_status", :controller => "ProjectStatus", :action => "show", :method=>'get' ) do %>
<%= select_tag 'project', options_from_collection_for_select(#projects,"id", "name"),:onchange => "this.form.submit();" %>
<% end %>
<%
if !#rows.nil?
#rows.each do |row|
end
%>
<%= row[:name] %>
<% end %>
what i basically want to achieve is this - based on the selected value from select tag
i want to display information(on the same view page) of selected item from the database
First of all you should write <%= form_tag not <% form_tag
Than, it's strange that your extract for select field id(options_from_collection_for_select(#projects,"id", "name"))
but in method show you search record by field lookup
You can use something of this sort to get this issue fixed
<%= check_box_tag "projectids[]",project.id %>
This would display the checkbox against each entry. And the checkbox is linked to the project object by its id. In the controller method, you will receive the project ids.
Then just directly use the submit tag to pass the parameters to the method.
I have a partial in my rails app that loads the vote form (It's just a select with numbers ranging from 1-5). I'm now making another partial that loads up if the user has already voted it's suppose to be an EDIt partial where you get selected the value that you voted and if you wanted to you could change it. But for some reason is not working, here's the code for it.
#Finds the vote by passing the user and the article
<%= #vote = Vote.find_vote_by(current_user,#article) %>
#Renders the partial with the vote variable loaded up with the vote that was found
<%= render :partial => "votes/edit_vote", :locals => {:vote => #vote} %>
And this is the partial
<% form_for(vote, :url => {:controller => 'votes', :action => 'edit'}) do |f| %>
<%= error_messages_for :vote %>
<p><%= f.hidden_field :article_id %></p>
<p>
<%= f.label :value, "Value for the vote: "%>
<%= f.select :value, {"1" => "1","2" => "2","3" => "3","4" => "4", "5" => "5"}, :selected => vote.value %>
</p>
<p>
<%= f.submit "Cloud-it!" %>
</p>
<% end %>
But for some reason the vote variable is not containing anything not the article_id, nor the value method, any ideas?
EDIT1: Per request here's what's debug #vote is outputting (It it indeed a sane value)
attributes:
created_at: 2010-09-02 14:39:04
updated_at: 2010-09-02 14:39:04
id: 1
value: 4
article_id: 1
user_id: 1
attributes_cache: {}
EDIT2
I tried clearing the partial code, in order to output this:
<%= debug #vote%>
<%= #vote.value %>
If i debug #vote it comes out perfect, will all the attributes and such. But whenever i add the second line it, It's not working it tells me that there's no .value, i tried .id, .article and nothing is as if it didn't exist. Any ideas?
EDIT3
Here's the vote by
named_scope :find_vote_by, lambda {|user,article| {:conditions => {:user_id => user, :article_id => article}}}
The reason behind it, it's that named scopes actually return named scopes, and you can't access the attributes just like it were a Vote class. I fixed this by changing the way to retrieve the vote and just forgetting about using that named scope. I accomplished it by using:
<% #vote = current_user.votes.find_by_article_id(#article)%>
which is a Rails method and actually returns a vote class. Then i just passed it to the partial and the magic worked!
Thank you so much to thenduks, without his help i couldn't had done it.
So first thing to fix is this line:
<%= #vote = Vote.find_vote_by(current_user,#article) %>
Should be:
<% #vote = Vote.find_vote_by(current_user,#article) %>
The former is for outputting in ERB and the latter is for executing arbitrary ruby code.
Next, put a line below that like so:
<%= debug #vote %>
And make sure it's a sane value. If not, paste the definition of your Vote class method find_vote_by.
EDIT: In that case it's probably just because using :locals => {...} makes instance variables, so you want #vote in your partial with the form.