NoMethodError in Browse#home - ruby-on-rails

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.

Related

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

How to pass multiple values to the create action in Rails?

In my app I have User, Post and Comment models.
When a User wants to comment on Post the new action from the Comments controller takes over. The Post (to be commented on) is shown and the User enters his Comment.
However, when the User submits, I want to pass the Post.id and the Comments.content to the create action. How do I do that?
Here is the comments/new.html.erb
<%= form_for #comment do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit "Done" %>
</div>
<% end %>
Thanks to all of you. I did the nested routing and my new.html.erb now has
<%= form_for [#post,#comment] do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<% f.hidden_field :post %>
<div class="field">
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit "Done" %>
</div>
<% end %>
However I get: undefined method `comment' and I cant figure that bugger out.
My guess is that each Comment must belong to a Post If that's the case then this seems like the perfect candidate for nested routes. http://guides.rubyonrails.org/routing.html#nested-resources
resources :posts do
resources :comments
end
So in your case both the post id and the comment id would be part of the URL:
# Will submit to a URL like /posts/1/comments
# or /posts/1/comments/1
<%= form_for [#post,#comment] do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit "Done" %>
</div>
<% end %>
You would need to handle the post_id in your comments controller actions.
First of all you have to pass Post.id to the comments new action. Something like
link_to "Add comment", new_comment_path( params[ :id ] )
I assume that you're following conventions so params[ :id ] is Post.id. Later in your Comment#create instantiate new comment with
#comment = Comment.new( :post_id => params[ :id ] )
which will create comment related to the post. Finally form for new comment
<%= form_for #comment do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= f.hidden_field :post_id %>
<div class="field">
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit "Done" %>
</div>
<% end %>
In the view (using HAML)
=form_for( #comment, :as => :comment) do |f|
=f.hidden_field :post_id
=f.hidden_field :user_id
=f.text_area :comment
=f.submit "Submit"
and in the Comment#new controller:
#comment = Comment.new(:user_id => #user.id, :post_id => #post.id)

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

Ajax Question - Ruby on Rails - Error: Couldn't Find Post without an ID

Ok, I am new to rails so I hope I explain this well. I seem to have a problem with a form collecting the proper information. Work in one spot and not another. Here is the situation.
I have created a blog with three tables, posts, comments, and votes. I have comments working correctly and votes working partially. Here is what I mean.
The /posts/show.html.erb file basically shows the post, the associated comments and it shows how many votes that post has. I am working the votes but using a form with a hidden field that delivers a value of '1' along with the post_id to the table. I am then using AJAX to return the #post.votes.count. This all works great.
I am trying to use this same vote/return mentioned above but on the /posts/index.html.erb page. In the index page I have the <% #post.each do |post| %> doing two things. First render :partial => post %> and then I have the same code that I am using on the show.html.erb page. The error that I am getting from the terminal is
ActiveRecord::RecordNotFound (Couldn't find Post without an ID):
app/controllers/votes_controller.rb:4:in `create'
I am guessing this has to do with it being part of the index record set which doesn't have a unique post param associated with the page. I am hoping there is a simple fix. Here is the code:
This is the /posts/show.html.erb code that is working correctly:
<div id="backto"<%= link_to 'Back to all BattleCries', posts_path %>
</div>
<%= render :partial => #post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="vote">
<div id="votes">
<%= #post.votes.count %> People liked this BattleCry.
</div>
</div>
<div id="votebutton">
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
<% end %>
</div>
</div>
</div>
<div id="beltcommentsbottom">
</div><br/><br/>
<div id="belt">
<div id="belttext">
<p5>Add a Comment</p5>
<% remote_form_for [#post, Comment.new] do |f| %>
<p>
<%= f.text_area ( :body, :class => "commentarea") %>
</p>
<%= f.submit "Add Comment"%>
<% end %>
</div>
<div id="beltbottom">
</div>
</div><br/>
<div id="comments">
<%= render :partial => #post.comments %>
</div>
<br/>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
<br/>
<br/>
<br/>
Here is where it is in the index page:
<% #posts.each do |post| %>
<%= render :partial => post %>
<%= render :partial => #post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="vote">
<div id="votes">
<%= post.votes.count %> People liked this BattleCry.
</div>
<%= link_to "Comments (#{post.comments.count})", post %>
</div>
<div id="votebutton">
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
<% end %>
</div>
</div>
</div>
<br/>
<br/>
<br/>
<% end %>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
<br/>
<br/>
<br/>
Here is the votes_controller.rb
class VotesController < ApplicationController
def create
#post = Post.find(params[:post_id])
#vote = #post.votes.create!(params[:vote])
respond_to do |format|
format.html { redirect_to #post}
format.js
end
end
end
Here is the /votes/create.js
page.replace_html :votes, :partial => #vote
page[#vote].visual_effect :highlight
Lastly, here is the partial, /_vote.html.erb:
page.replace_html :votes, :partial => #vote
page[#vote].visual_effect :highlight
I hope this makes sense. Let me know if I need to clarify anything.
I imagine this is causing problems. You didn't post your posts controller, but does the index action set #post?:
<%= render :partial => #post %>
Looks like I just needed to change the following in the <% #posts.each do |post| %> section of the index.html.erb file.
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
to
<% remote_form_for [post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>

Resources