Rendering form partials from other controllers - ruby-on-rails

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

Related

Rendering error messages not working on just one page

I can't get error messages to display on this page. They display fine for any other page but not this one
mod_approval.index.html.erb
<% #check_category.each do |category| %>
<%= form_for([#guide, Guide.friendly.find(#guide.id).categories.new], url: guide_mod_panel_approve_category_path, method: :post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= hidden_field_tag :check_id, category.id %>
<%= f.hidden_field :name, :value => category.name %>
<%= f.submit "Approve" %>
<% end %>
<% end %>
mod_approval_controller
def mod_add_category
#guide = Guide.friendly.find(params[:guide_id])
#check_category = CheckCategory.where(guide_id: #guide.id).all
#category = Guide.friendly.find(#guide.id).categories.new(category_params)
if #category.save
flash[:success] = "Game category added succesfully!"
redirect_to guide_mod_panel_mod_approval_index_path(#guide)
else
render 'mod_approval/index'
end
end
routes
match '/guides/:guide_id/mod-panel/approve/category' => 'mod_approval#mod_add_category', :via => :post, as: :guide_mod_panel_approve_category
match '/guides/:guide_id/mod-panel/approve' => 'mod_approval#index', :via => :get, as: :guide_mod_panel_mod_approval_index
Not too sure why they aren't rendering I tried changing <%= render 'shared/error_messages', object: f.object %> to <%= render partial: 'shared/error_messages', object: f.object %> but that gives the error
undefined local variable or method `object' for #<#<Class:0x007ffdbcd1c3a8>:0x007ffdbcbdd320>
on this line <% if object.errors.any? %>
This error rendering setup was made from Michael Hartls rails tutorial and as I said I works fine for every other form but this one.
The reason is that when you render the form, you instantiate a new Category:
form_for([#guide, Guide.friendly.find(#guide.id).categories.new],
You need to give form_for the Category instance that has the errors (#category from your controller). So I would change your form to this:
form_for([#guide, #category],
And then in your #new method make sure you set it up:
#category = #guide.categories.build

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.

Ruby On Rails - Simple_fields_for with nested_form, pass variable

Is it possible to pass variable when using nested form with Simple form?
Like
<%= simple_nested_form_for(#foo) do |f| %>
...
<%= f.simple_fields_for :bar %>
<%= f.link_to_add :bar do %>
Add bar
<% end %>
<% end %>
I have tried
<%= f.simple_fields_for :bar, :locals => {:baz => 'baz'} %>
but it wont pick it up in the partial.
Partial: _bar.html.erb
<%= baz %>
Simple Form Gem
Nested Form Gem
I was looking for the same thing and never did see an answer anywhere, so I'm posting an example of what worked for me:
<%= f.simple_fields_for :answers do |answers| %>
<%= render 'answer_fields', { f: answers, question: #question } %>
<% end %>
Also, if you want to access the current object from the collection being rendered within the partial, use f.object within the partial.
To pass variables to partials, you use the :locals option:
<%= render partial: "my_awesome_partial", locals: {variable: 5, baz: 'baz'} %>

passing variable to simple form symbol

this is my _form_item partial in which I used symbol :order_item
<%= simple_form_for :order_item do |f| %>
.....
<% end %>
here is my view in which I want to render that partial:
<%= content_tag_for :tr , #order.order_items do |i| %>
<div class="hide">
<%= render :partial => "form_item" %>
</div>
<% end %>
How can I pass "i" object to :order_item?
UPDATE:
I prefer to keep it ":order_item" instead of changing it to something like "foo".
You should have:
<%= render partial: 'form_item', locals: {order_item: i} %>
or a shorthand:
<%= render 'form_item', order_item: i %>
And in your form_item partial you should have:
<%= simple_form_for order_item do |f| %>
...
Just do
<%= simple_form_for item do |f| %>
.
.
<% end %>
and render the partial via
<%= render :partial => 'form_item', :locals => { :item => i } %>
You can pass i as a locals to the partial like follows:
<%= render :partial => "form_item", :i => i %>
and you'll have i available in the _form_item partial.
Update:
Local variables passed to partials are variables and not symbols. You could keep the name order_item like follows:
<%= render :partial => "form_item", :order_item => i %>
And update your order_item partial as follows:
<%= simple_form_for order_item do |f| %>
.....
<% end %>
I think I understand why you do not want to change :order_item to just order_item. I think you are going to have to update your other calls to pass in local variable order_item where ever you are making a call to this partial.

rendering a different template in 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 %>

Resources