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.
Related
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
Every time I try to render something that is located from a different view, I get a NoMethodError: undefined method `each' for nil:NilClass.
It happens when I put the following code in the view I want to render stuff on:
views/uploads/myuploads.html.erb
<%= render template: 'guitar_sounds/index' %>
And it tells me that the error seems to be in a particular block of code where the template is located:
views/guitar_sounds/index.html.erb
<% #guitar_sounds.each do |sound| %> <!-- Error here -->
<%= render "guitar_sound", sound:sound %>
<% end %>
However, when I load that page view on its own, I get no errors.
Can someone help me?
Loading a partial does not automatically hit a controller method. By that, it sounds like the only controller method being run is uploads#myuploads, but your #guitar_sounds variable is being defined in guitar_sounds#index. I'd simply define the #guitar_sounds variable in your UploadsController
UploadsController < ApplicationController
def myuploads
# here is where #guitar_sounds needs to be defined
#guitar_sounds = GuitarSound.all
end
end
Let's say you needed #guitar_sounds in lots of methods, you could define it in a before_action
UploadsController < ApplicationController
before_action :set_guitar_sounds
def myuploads
# normal controller code
end
private
def set_guitar_sounds
#guitar_sounds = GuitarSound.all
end
end
Now #guitar_sounds will be set for every method in the UploadsController
Your template guitar_sounds/index expects #guitar_sounds to be defined, and be able to iterate over its items.
If you reuse the template without assigning any values to #guitar_sounds, by default it will be nil, thus you can see the error.
Hope it clarifies a bit the problem!
guitar_sounds/index expects #guitar_sounds to be defined, aka, not nil, so it can iterate over its items.
You should, instead, use local variables.
<%= render template: 'guitar_sounds/index', guitar_sounds: #guitar_sounds %> #or other # variable
And at your view:
<% guitar_sounds.each do |sound| %>
<%= render "guitar_sound", sound:sound %>
<% end %>
Now guitar_sounds (note the missing #) is a local variable that you pass to the render function!
EDIT: Check rails documentation about this: Passing Local Variables to partials/templates.
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 %>
I have a Project Index View that shows all the projects in an app
I want that view to show if the user signed in is a member or not....
In the Project Index View I have:
<% if teammember? %>
<td>Request to Join</td>
<% else %>
<td>Already Joined</td>
<% end %>
Then in the project's controller I have
def teammember(projectid)
do some stuff.....
end
But this gives me a "undefined method `teammember?"
You don't include the teammember method in the controller, you put that in the helper file (app/helpers/project_helper.rb)
module ProjectHelper
def team_member?(project_id)
# include other logic here
true
end
end
Then in any view that your Project controller renders, you can do:
<% if team_member?(project.id) %>
This is a team member.
<% else %>
This isn't a team member.
<% end %>
If this is a controller method that you need to access in the view, you can make it available like this:
class ProjectsController < ActionController::Base
helper_method :team_member?
end
This is essentially the same as if you had defined the method in helpers/projects_helper.rb
Just make sure you call the methods the same: your example shows one with a question mark, and one without.