rendering a different template in rails - ruby-on-rails

i've a view with a
<%= render :partial => #list.items%>
To show the this command called the _item.html.erb. Right?
<div class="well">
<%= image_tag item.photo.url(:small) %><br>
<b>Title</b> <%= item.title %><br />
<b>Description</b> <%= item.description %>
</div>
This works fine.
Now i have an other template called _ilist.html.erb where i need some of this data.
I try to render this by adding the template option. But the other template will not used. The first template will called anytime.
<%= render :partial => #list.items, :template => 'items/ilist' %>
Is there any option to call the other _ilist-template?
Thanks for your help

I believe you need:
<%= render :partial => 'items/ilist', :collection => #list.items, :as => :item %>

Related

NoMethodError in Browse#home

I have the following view file in browse/home
<% if current_user %>
<% #post = Post.new %>
<%=render :partial => 'posts/newpost.html.erb'%>
<div id="postsfeed">
<%= render :partial => 'post.html.erb', :locals => { :posts_streams => #posts_streams } %>
</div></br>
<% end %>
posts/newpost.html.erb is
<%= form_for(#post) do |f| %>
<div class="fields">
<%= f.label "Post" %><br />
<%= f.text_field :post %>
</div>
<div class="actions" id="refreshposts">
<%= f.submit("Post") %>
</div>
<% end %>
models/post.rb is as follows
class Post < ActiveRecord::Base
attr_accessible :post, :posted_by, :posted_by_uid
end
logs:
Rendered browse/home.html.erb within layouts/application (42.9ms)
Completed 500 Internal Server Error in 72ms
ActionView::Template::Error (undefined method `post' for #<Post:0x00000003ad7cf0>):
1: <%= form_for(#post) do |f| %>
2: <div class="fields">
3: <%= f.label "Post" %><br />
4: <%= f.text_field :post %>
5: </div>
6: <div class="actions" id="refreshposts">
7: <%= f.submit("Post") %>
app/views/posts/_newpost.html.erb:4:in `block in _app_views_posts__newpost_html_erb__281623928071728826_30583780'
app/views/posts/_newpost.html.erb:1:in `_app_views_posts__newpost_html_erb__281623928071728826_30583780'
app/views/browse/home.html.erb:3:in `_app_views_browse_home_html_erb___3611275753955382446_40484320'
when I initiate the server. It returns NoMethodError in Browse#home. Please suggest a way to resolve this
Thank you
Probably, there should be a post column in posts table in database
Also following points should be noted as per [Rails standards](http://guides.rubyonrails.org/layouts_and_rendering.html#Using Partials):
Avoid to mention extension .html.erb, So change the code from:
<%= render :partial => 'posts/newpost.html.erb'%>
<%= render :partial => 'post.html.erb', :locals => { :posts_streams => #posts_streams } %>
to:
<%= render :partial => 'posts/newpost'%>
<%= render :partial => 'post', :locals => { :posts_streams => #posts_streams } %>
Also the partial begins with an underscore, so place partial in views folder as posts/_newpost.html.erb.
check your routes and add
resources posts
or to add partial take example your file is '_partial.html.erb'
<%= render controller/partial %>
template error is the error which tells you that there is no such page you are rendering to, so do check whether it exists to the same path or not.

partials inside a view undefined method errors

I am adding partials to a show view, but I am getting errors of undefined methods that are inside the partials that are being called. I use the partials in other places and it works fine. I think I need to define a local variable, but I am not sure which one
user/show.html.erb
<h2>Copy To Approve</h2>
<div id="basic_details" class="idea-show-columns">
<%= render :partial => "/ideas/idea_basic_show" %>
<%= render :partial => "/ideas/comments" %>
<%= render :partial => "/ideas/mockups"%>
</div>
<div id="copy_details" class="idea-show-columns">
<%= render :partial => "/ideas/copy_show" %>
</div>
partial: ideas/idea_basic_show.html.erb
<fieldset data-model="idea-basic" class="idea-edit">
<h2>Basic Idea Specs</h2>
<div data-attribute="product_sku" class="edit-field">
<%= label :sku, "Product SKU" %>
<%= f.text_field :sku %>
</div>
<div data-attribute="working_name" class="edit-field">
<%= label :working_name, "Working Name" %>
<%= f.text_field :working_name %>
</div>
<div data-attribute="priority" class="edit-field">
<%= f.label :priority, 'Priority Level' %>
<%= f.select :priority, Idea::PRIORITIES.collect{ |level, label| [label, level]} %>
</div>
<% if current_user.has_overlord_access? %>
<div data-attribute="overlord_id" class="edit-field">
<%= f.label :overlord_id, 'Sign Off' %>
<%= f.select :overlord_id, User.overlords.collect{|o| [o.full_name, o.id]},
:include_blank => true %>
</div>
<% end %>
<br data-clear="all" />
<div data-attribute="working_description" class="edit-field">
<%= label :working_description, "Working Description" %>
<%= f.text_area :working_description %>
</div>
</fieldset>
the errors I am getting in this partial specifically is :
undefined method `sku' for nil:NilClass
Like I said I think I need to define a local in my partial I just don't know the syntax
The locals syntax like this
<%= render :partial => "/ideas/copy_show", :locals => {:f => f} %>
now use f in your partial page

Rails3 form partial reuse

The following is the Rails std form that works.
<div id="content-form">
<%= form_for #passion, url: passion_path do |f| %>
<%#= render :partial => "form", :f => f %>
<div class="alternate">
<%= f.label "Status" %>
<%= f.text_field :status %>
</div>
<% end %>
</div>
Now I extract the partial to _form.html.erb with the following:
<div class="alternate">
<%= f.label "Status" %>
<%= f.text_field :status %>
</div>
And update the form as:
<div id="content-form">
<%= form_for #passion, url: passion_path do |f| %>
<%= render :partial => "form", :f => f %>
<% end %>
</div>
But now this throws an error complaining about the f variable.
undefined local variable or method `f' for #<#<Class:0x0000012d51d3e8>:0x0000012d796ef8>
Extracted source (around line #2):
1: <div class="alternate">
2: <%= f.label "Status" %>
3: <%= f.text_field :status %>
4: </div>
Why is this? Its pretty Rails basic.
One thing to consider that the resource #passion is singular. i.e. resource :passion (user has_one passion)
Due to that, I've to use explicit url: passion_path in the form_for ....
Btw, using Rails 3.2.9
try this
<div id="content-form">
<%= form_for #passion, url: passion_path do |f| %>
<%= render :partial => "form", :locals=>{:f => f } %>
<% end %>
</div>
You are using the :partial option, which means you have to pass local variables in with :locals:
<%= render :partial => "form", :locals => { :f => f } %>
The less verbose option is:
<%= render "form", :f => f %>

Rendering form partials from other controllers

I have a few shared partials that I can render from any controller fine however I am having a bit of trouble rendering form partials from another controller. I am wanting to be able to add notes to my contacts
In my contacts/show.html.erb i have the following
<% render :partial => "notes/form", :note => Note.new %>
In my notes/_form.html.erb i have the following
<%= form_for #note do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<p>
<%= f.label :content %><br />
<%= f.text_field :content %>
</p>
<p>
<%= f.label :contact_id %><br />
<%= f.number_field :contact_id %>
</p>
<p><%= f.submit %></p>
<% end %>
However I get the error:
Showing /Applications/Rails/apps/saas31/app/views/notes/_form.html.erb where line #1 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for #note do |f| %>
2: <%= render 'shared/error_messages', :object => f.object %>
I'm starting to get the hang of rails but having a few small frustrating problems as to be expected when learning anything new i suppose. Anyone have any ideas?
Your local variables should be passed through in a locals hash.
<% render :partial => "notes/form", :locals => {:note => Note.new} %>
Read section 3.4.4 here.
Your partial also shouldn't use instance variables, change the following:
<%= form_for #note do |f| %>
to:
<%= form_for note do |f| %>
edit
If you want to use an instance variable, you can do the following:
<% render :partial => "notes/form", :locals => {:note => #note} %>
Ran into the same issue and this post was helpful in solving. Adding my notes and hopefully it will help someone else :)
I had a a Users controller and a _form.html.erb that rendered fine when I would access the /users/new page. I was trying to render the form as a partial from my /layouts/application.html.erb, as I wanted to give users the ability to create a new user from any page.
I ended up creating a new method (new_user) in application_helper.rb. Here is the code:
def new_user
User.new
end
I then render the partial from application.html.erb with:
<%= render :partial => 'users/form', :locals => {:user => new_user} %>

Adding javascript handlers to remote form object after injection with RJS

I have a page form, where i want the user to add nested forms by clicking on a remote link ('add photo', f. ex). Upon clicking the button I inject the nested forms with RJS. The injected forms are remote forms, but after injection the forms have no javascript events attached so they just submit with http to the controller of the nested object. Beside that, everything works fine.
My question is:
Is it possible to trigger revalidation of javascript of remote forms via RJS, after injection??
That way my nested forms would get the proper javascript event handling and submit with AJAX.
My page form (_form.html.erb):
<% javascript 'ckeditor/ckeditor' %>
<%= form_for #page, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :active %><br />
<%= f.check_box :active %>
</p>
<p>
<%= f.label :homepage %><br />
<%= f.check_box :homepage %>
</p>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description, :rows => 5, :cols => 80 %>
</p>
<p>
<%= f.label :text %><br />
<%= f.text_area :text, :class => 'html' %>
</p>
<p>
<%= f.label :picture_class %><br />
<%= f.select :picture_class, ['left','right','full'] %>
</p>
<% unless #page.new_record? %>
<h2>Photos</h2>
<%= link_to 'Add photo', add_photo_page_url(#page), :remote => true %>
<div id="photos">
<%= render :partial => 'shared/forms/photo', :collection => #page.photos %>
</div>
<h2>Snippets</h2>
<%= link_to 'Add snippet', add_snippet_page_url(#page), :remote => true %>
<div id="snippets">
<%= render :partial => 'shared/forms/photo', :collection => #page.snippets %>
</div>
<h2>Downloads</h2>
<%= link_to 'Add download', add_download_page_url(#page), :remote => true %>
<div id="downloads">
<%= render :partial => 'shared/forms/download', :collection => #page.downloads %>
</div>
<h2>Paragraphs</h2>
<%= link_to 'Add paragraph', add_paragraph_page_url(#page), :remote => true %>
<div id="paragraphs">
<%= render :partial => 'shared/forms/paragraph', :collection => #page.paragraphs %>
</div>
<% end %>
<p><%= f.submit %></p>
<% end %>
<%= javascript_tag 'CKEDITOR.replaceAll("html");' %>
Controller code to build the nested photo object: (page_controller.rb):
def add_photo
#page = Page.find(params[:id])
#photo = #page.photos.build
end
RJS code that gets executed by clicking 'add photo' (add_photo.js.rjs):
page.insert_html :bottom, :photos, :partial => 'shared/forms/photo', :object => #photo
page.visual_effect :highlight, :photos, :duration => 2
The partial/form that gets injected with RJS (_photo.html.erb):
<%= form_for [#page, photo], :remote => true do |f| %>
<p>
<%= f.check_box :active %>
<%= f.text_field :name %>
<%= f.file_field :photo %>
<%= f.submit 'Save' %>
</p>
<% end %>
Any help would be appreciated. I would like to solve this with nested forms (not nested attributes) and RJS, but was unable to google my way to documentation on this.
Thanks!!
-- Jakob
If you want to stick with RJS, you'll need to look into the built-in javascript to see how they're registering everything. They probably just do it on DOM ready and bind to the submit() event. It's most likely encapsulated in a method which looks for anything marked :remote => true and binds to the submit() event so it will submit via AJAX. You can execute the same logic but isolate it to your new form. So maybe your partial could have a JavaScript tag which executes that logic for that form, based on the id, or based on the classname, ignoring everything but the most recently added form.

Resources