I'm trying to render an action in my application.html.erb layout file to display it as a modal box using some jquery scripts. I've heard that i can use render :template => 'spots/new' but it looks like this method is not rendering an action but just a view file.
spots#new
def new
#spot = Spot.new
end
new.html.erb
<%= form_for(#spot) do |f| %>
...
<% end %>
The problem is that when i'm trying to render spots#new with render :template => 'spots/new', i'm getting undefined method 'model_name' for NilClass:Class error. Have you any idea what am i doing wrong ? Thanks in advance
You are correct, render :template => 'spots/new' just renders your view template, it does not call spots#new. You should create #spot instance variable before rendering the template.
In your case probably following code will work:
<% #spot ||= Spot.new %>
<%= form_for(#spot) do |f| %>
...
<% end %>
Related
I'm currently having the issue for when I try to use a partial inside of invoices/_form.html.erb, it goes into parts/_index.html.erb and breaks.
Inside of the parts_controller I have:
def _index
#parts = Part.all
end
#unsure if this is needed
Inside of invoices_controller I have:`
def new
#invoice = Invoice.new
#parts = Part.all
end`
Inside of invoices/_form.html.erb I have:
<%= render :partial => "parts/index" , :part => #parts %>
And inside of invoices/new.html.erb I have:
<h1 style="padding-left:120px">New Invoice</h1>
<%= render 'form', invoice: #invoice, part: #parts %>
<%= link_to 'Back', invoices_path, class: "btn btn-default col-md-2" %>
So what this code is attempting to do is display the index page of parts so the user is able to see all current parts they have in stock, and how many of that part is in stock. The parts/index page is the exact same as the the default index page for parts, but it just has a link removed.
The line of code that gives me an issue in parts/index is:
<% #parts.each do |part| %>
And what's confusing me about that is that I should be passing it an object that has data inside of it, since it's declared in both the controller for parts, and the invoice controller. Am I missing something super simple with my syntax, or is what I'm trying to do not the right way to do it? I'm still a noob to rails, so sorry if what I'm trying to get across doesn't make too much sense.
so here is the problem:
<%= render :partial => "parts/index" , :part => #parts %>
you are sending :part to your _index.html.erb partial while using #parts
you need to update your render call to following:
<%= render :partial => "parts/index" , locals: {parts: #parts}%>
and your loop to:
<% parts.each do |part| %>
you provide to _index.html.erb variable part but try to render #parts.
1. you don't need method _index, when your patial _index.html.erb render that not get variable #parts from method _index. I think it's wrong.
2. You need to render in _index.html.erb variable which it's provided from _form
<% part.each do |part_| %>
Note: Apologies if this is a duplicate, I couldn't find an answer. Warning: Newbie to RoR, the answer is probably incredibly obvious.
I have a partial, _show_address_on_map, which shows the location of a person on a map. However, this person can be an #employee or a #client, and can be called from the employee_addresses controller or the client_addresses controller. Depending on which of the two it is, some things need to be changed within the partial.
In employee_addresses/show.html.erb I call the partial with
<%= render :partial => ".../show_on_map", currentUser: #employee %>
In client_addresses/show.html.erb I call the partial with
<%= render :partial => ".../show_on_map", currentUser: #client %>
Now in the partial (_show_address_on_map) I try to do an if-statement on currentUser:
<% if currentUser.is_a?(Client) %>
#do something with #client
<% else %>
#do something with #employee
<% end %>
This gives me the error "undefined local variable or method 'currentUser'"
How do I correctly define currentUser, so that it can be either #employee or #client as described? Or am I doing something else wrong?
<%= render :partial => ".../show_on_map", locals: {current_user: #client}%>
:)
Also as a ruby/rails convention, use the under_score rather than camelCase
You can use the :object to send a data into the partial, and that will define a variable with the same name as the partial
<%= render :partial => ".../show_on_map", :object => #client %>
So in your partial, you can reference the :object send with the name of the partial _show_address_on_map, you can do something like this:
<% if show_address_on_map.is_a?(Client) %>
#do something with #client
<% else %>
#do something with #employee
<% end %>
And that will contain the #client sent in :object, so you can control what action do in the partial.
I have a welcome.html.erb page with welcome_controller. On this page I try to render a partial which belongs to Screen model, but it returns NoMethodError: undefined method 'each' for nil:NilClass. Here's the code:
welcome.html.erb:
<%= render 'screens/all` %>
_all.html.erb:
<%= #screens.each do |screen| %>
<%= link_to screen do %>
<img src="">
<% end %>
<% end %>
screens_controller.rb:
def all
#screens = Screen.all.order('created_at ASC')
end
Hey you are doing wrong.
def all
#screens = Screen.all.order('created_at ASC')
end
That method you have in screens_controller.rb
You need to write the instance inside the welcome method at welcome_controller.rb, That way it will not give error to you.
That way you can access the partial and one more thing pass the variable in locals with partial and use that instead of the actual instance.
<%= render :partial =>'screens/all`, :locals => {:screens => #screens} %>
The instance variable you're using (#screens) should be coming from whatever controller action renders welcome.html.erb. You should then pass it explicitly as a local (like Bharat shows in his answer).
you Forgot to pass the variable
Try using
<%= render 'screens/all',:screens => #screens %>
I'm new to ruby on rails.
In views/events I have "_form.html.erb" which is rendered in "new.html.erb" by this code:
<%= render "form" %>
Now I want to render "_form.html.erb" in "index.html.erb" which is in the same folder(views/events).
But I get the error "missing template".
I guess I have to add some thing to controller, please help me to render form in other pages of views...
You "usually" don't render a form in an index action. Most form partials are setup semantically to expect a #my_resource, but if you're doing everything the rails way you're not going to have a instance variable during your index action. There's a number of ways you can do this but this is probably the quickest.
You probably have some collection (let's pretend you're using books) in your index action:
#views/books/index.html.erb
<% #books.each do |book| %>
...
<%= render "form" %>
...
<% end %>
You can just set an instance variable somewhere prior to rendering the form:
#views/books/index.html.erb
<% #books.each do |book| %>
<% #book = book %>
...
<%= render "form" %>
...
<% end %>
Another way to do it would be through passing in some locals to a partial. You'd have to change all of your references in _form to use a local variable instead. Then you can call render like this:
<%= render :partial => "form", :locals => {:book => book } %>
You can try
<%= render "events/form" %>
I had this problem before and this solved
In my rails view(index.html.erb), i have following structure
<div>
<%= render :partial => "create" %>
</div>
<div>
<%= render :partial => "show" %>
</div>
In controller's create action if i use
def create
render :update do |page|
page.replace_html 'show', :partial => 'show'
end
end
I get below error
undefined method `map' for nil:NilClass
because the instance variable doesnt get initialized from controller
If i use
def create
render :file => "_show.erb"
end
I get template missing error. because the base layout file is not getting rendered.
I basically want to update the partial on create event.
Any suggestions would be of great help.
The solution :layout => false works.
But, newly added entry doesnt come until page refresh
#entry = Model.find(:all)
in the _show.html.erb doesnt get updated
You can try like this it will surely works..............
def create
render :partial => "show" ,:layout => false
end
but your page should be name like
_show.html.erb