I want to send a variable from controller to partial file.
My controller name is center_controller.rb and the partial file name is _view_center.html.erb.
In controller, I have #notes variable. When I give <%= #notes %>, Iam not getting the values,nil is coming. Can you help me on that?
center_controller.rb
def view
#notes = Notes.all render"view_center.html.erb"
end
and In my view_center.html.erb
<%= #notes %>
But Iam not getting #notes values, it is nil.
Thanks in Advance !!!
IMPORTANT UPDATE!
This is wrong
#notes = Notes.all
This is right
#notes = Note.all
Watch out ! When you call a class you want the class name in Singular
First of all you need to name the view to match the controller method.
View file names, by default, match the controller and action that they are tied to.
If you call your view (view_center.html.erb) you should name your controller method (view_center) example:
#center_controller.rb
def view_center
#notes = Note.all
end
#views/center/view_center.html.erb
<%= #notes %>
When you want to render a partial that uses a instance variable, you should do:
<%= render 'view_center', notes: #notes %>
The [notes: #notes] sends the variable to the partial
In the partial file you need to do this:
<% notes.each do |note| %>
<% end %>
Otherwise, take a look at Rails naming conventions, because usually, controller filenames are plural:
https://gist.github.com/iangreenleaf/b206d09c587e8fc6399e
centers_controller.rb
users_controller.rb
etc
Related
I have retrieved some records based on condition in a hash - #special_products. Now I want to pass the hash to a non-restful action(:special)/ of the same controller so that I can view the products.
I've tried this but how can link_to pass hash and how should the value be retrieved in action: special? which is in the same products_controller?Many thanks.
products_controller.rb
def show
#special_products = Product.by_company
end
show.html.erb
<%= link_to "Special Products", special_path(:anchor => "#{#special_products}") %>
If you're hitting the show action of the Products controller, you should be showing a product.
If you want to show a product in a special way, use the same show action, but render a different view for it.
def show
#product = Product.find(params[:id])
render #product.special? ? 'special_show' : 'show'
end
If you want to list some products in a different way (a filtered collection), you want to use an index. E.g. the products#index action.
def index
#products = Products.not_special
#special_products = Products.way_special
end
# app/views/products/index.html.erb
Special Products:
<%= #special_products.pluck(:name).to_sentence %>
Finally, note that the parameters you pass to link_to end up in the linked URL, which means that your example link_to is going to render something like #<Array []> in the href attribute.
In my Rails app, I get to render an erb file from a controller and from another erb file like so:
# a_controller.rb
class AController < ApplicationController
def index
render 'lista'
end
# b_controller.rb
class BController < ApplicationController
def index
render 'listb'
end
# lista.html.erb
<%= render 'list' %>
# listb.html.erb
Some content that I want to keep
<%= render 'list' %>
# _list.html.erb
Hello
As you can see lista is only rendering the partial _list.
What I wanted to do was to call <%= render 'lista' %> from within listb.html.erb.
But when I do so, I get an error message saying that the partial _lista is not found.
Do you have an ideas?
lista.html.erb is a page, not a partial. Your code is looking for a partial called lista but it does not exist.
I really think you should create a new partial called _lista, you can render it in lista.html.erb and listb.html.erb. Your controller can still render 'lista' which will render the partial _lista.
You need to create _list.html.erb partial (any file name) file for access it with in lista.html.erb or listb.html.erb after that you can access as you want.
For example:-
# lista.html.erb
<%= render 'listb' %>
In lista.html.erb we can call listb (_listb.html.erb) file that are in current directory.
you should create a partial with name _lista.html.erb or you can change your file name from lista.html.erb to _lista.html.erb.
currently file lista.html.erb is not a partial that's why when you try to render lista partial, rails produce a error for you
#lista.html.erb
##assuming list is in lista/list.html.erb
<%= render 'lista/list' %>
Hi everyone at this time I created a new controller, view and helper called Menuprestacionessociales
I have all the files as I think it should be
menuprestacionessociales_controller.rb
class MenuprestacionessocialesController < ApplicationController
def index
#variable = "cualquier contenido"
end
end
menuprestacionessociales/index.html.erb
<%= #variable %>
So I have a simple variable in my controller called #variable but if I try to show the content in the index, it doesn't show anything
Put <%= #variable %> somewhere in the index.html.erb file.
To generalize my problem, I am using an API that returns an iterable object. Within that those is an id for each object. My controller looks like this:
class SearchController < ApplicationController
def index
#search = API.find(params[:query])
end
end
My view is something like this:
<% #search.each do |thing| %>
<h2><%= thing.attr2 if thing.attr1 %></h2>
<%= API.list(thing.attr2) %>
<% end %>
I have tried adding a method into
class SearchController < ApplicationController
def index
#search = API.find(params[:query])
def getList(attr2)
API.list(thing.attr2)
end
end
end
and adding index and self before the definition (ex: self.getList(attr2)) and calling it in all those variations in the view:
<%= getList(thing.attr2) %>
I am wondering where I am going wrong here. I have additionally tried to add in the helper_method line as I read in a few docs but it would not recognize it. Also, would this be the correct way to go about this style-wise? Having a hard time finding references for it makes me think this isn't standard practice.
The method I was trying to make is a helper method, and therefore, needs to go in the helper method for the controller.
I want to place my <%= form_for(#something) do |f| %> which is currently located in app/views/something/new.html -- inside multiple pages, so maybe in app/views/layouts/application.html.erb
How do I get the #something variable and the form to work properly there, or somewhere else -- since it's defined in the controller #new action of SomethingController, it only seems to be available in the appropriate new.html.erb view..
You can put the form anywhere, just provide an instance variable of #something in controller
The basic usage is here.
ThisThingsController
def show
#this_thing = foo
#that_thing = bar
end
end
# View
<%= #this_thing %>
<%= form_for #that_thing %>
Of course you can use partial to render the form, as long as you feed it with variable it needs.
Try
<%= form_for SomeThing.new do |f| %>
Without fully understanding what you are trying to accomplish, I'll make this suggestion.
Add a before filter to your ApplicationController (alternatively you could create a module and mix it in where needed). Then call the before_filter when needed. This example will always run the before filter:
class ApplicationController
before_filter :set_something
private
def set_something
#something = ... # Fill in the logic here
end
end
Then add your form where needed. You can even make it appear conditionally depending on whether #something is set.
<% if #something %>
# Form goes here
<% end %>