error updating partial in rails - ruby-on-rails

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

Related

How to render show action as a partial

I have a search form. when i search for a record, the result goes to the show page as expected , but i want the result to render on the same page where ever i put the form partial
I want to create a partial that will render the show action on the same page with the form
Here is the shipment controller
class shipmentsController < ApplicationController
def show
#shipment = Shipment.find_by_trackCode(params["trackCode"])
unless #shipment.present?
end
......
Home controller index.html.erb
<%=render 'shipments/form' %>
Views/shipments/_form.html.erb
<%= form_tag search_path, :controller => 'shipments', :action => 'show', :method => 'get' do %>
<%= text_field_tag :trackCode %>
<%= submit_tag 'Track' %>
<% end %>
The present code will dispaly the result on the show page but I want the search result to be rendered using a partial so that i can render it where ever i use the form partial.
How can i archive that ?
are you really sure it's a good idea to use your show view as a partial? now if you are... you could rename your file _show.html.erb and reference it passing the folder name <%=render 'shipments/show' %> but you'd also have to change the loop to call the model Shipment.all.each instead of #shipment
render template: 'shipments/show', locals: {shipment: #shipment}
use variable shipment instead of #shipment in show.html.haml

undefined method `each' for nil:NilClass while attempting to use a partial

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_| %>

Rendering a Partial from another Folder

I'm Trying to render a Partial from my App/view/pins folder to my
app/view/layout folder (My menu).
Basically i'm trying to render my _form.html.erb to a Modal which is
triggered with a button on my Menu.
I'm using Bootstrap.
Every time i give my modal the code:
<%= render 'pins/form' %>
i'm getting:
NoMethodError in Pins#index
undefined method `model_name' for NilClass:Class
I searched A LOT and EVERYWHERE but couldn't fix it..
Can anybody Help me?
(Git repo-> https://github.com/Theminijohn/Amphitryon)
Errors:
The Error Trace -> http://i.imgur.com/i4aVQNd.png
Try using this:
<%= render :partial => 'pins/form' %>
Edit
I looked at your code. In your pins/form partial you're referring to #pin instance variable, which may not be available in your partial. That's why your stack trace is referring to a NilClass.
I think you can try to pass #pin as a local partial variable in one of these ways:
<%= render :partial => 'pins/form', :locals => { :pin => #pin } %>
or (shorthand syntax)
<%= render "pins/form", :pin => #pin %>
Try replacing #pins in the simple form definition with current_user.pins.new
The shorthand render #pins is doing this:
render :partial => 'pin', :collection => #pins
whereby each pin is passed as a local variable.
However, your partial references the instance variable #pin which, unless explicitly assigned in the controller, will return nil:
<%= simple_form_for(#pin, #...snipped...
Change it to the local variable, like so:
<%= simple_form_for(pin, #...snipped...
How i Fixed it
Thank you for all your Help, without you guys i wouldn't have figured it out.
1.) I Changed the form from #pin to
current_user.pins.new
2.) Than, because the modal got rendered without the user being logged in and that caused an error i wrapped the modal in an if statement
<% if user_signed_in? %>
...form code...
<% end %>
and everything worked perfectly.

Rendering action

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 %>

Reloading rails view from another controller

Suppose I have the following view in my rails app
<% if is_subscriber? %>
<%= render :partial => 'subscriber_page' %>
<% else %>
<%= render :partial => 'payment_form' %>
<% end %>
The payment form submits a post-rquest to the controller show page. In my routes file I have:
match ':users(/:id)', :to => 'users#submit_payment', :via => :post
My submit payment helper method is:
def submit_payment
current_user.is_subscriber = true
show
end
However when I run this I get a submit_payment template missing error. How can I make sure that the users show page is simply reloaded when the helper method is finished.
It looks like you're trying to redirect to the show page. Try this:
redirect_to user_path(#user) # Note you need to send the show page an :id param
If you're just wanting to render a specific partial, do this:
render :partial => 'show'
However, if you want something more seamless, like submitting the form VIA Ajax, which gets back a partial and then on success dumps it in to a waiting DIV or something, you can do that as well, though with a bit more complexity.

Resources