undefined method `model_name' for Array:Class, Partial views - ruby

I am using partial view that is not working, in review#new action, I have a from and I am calling the post partial view :
<%= form_for #review do |f| %>
<div>
<%= field_with_error #review, :content do %>
<%= f.label :content %><br />
<%= f.text_area :content %>
<% end -%>
</div>
<%= field_with_error #review, :score do %>
<%= f.label :score %>
<%= f.text_field :score %>
<% end -%>
<p><%= submit_tag 'Post Review'%> </p>
<% end %>
</div>
<ul >
<%= render :partial => "post", :locals => {:review => #reviews} %>
</ul>
in Review#new controller :
def new
#review = Review.new(:restaurant_id => params[:restaurant_id])
#reviews = Review.all
end
and review#_post looks like:
<%= content_tag_for(:li, review) do %>
<p ><%= "#{review.first_name}" %></p>
<p ><%= review.content %></p>
<span >Posted at <%= review.created_at %> ago.
(<%= link_to 'Delete', post, :confirm => 'Are you sure?', :method => :delete %>)</span>
<% end %>
It gives an error:
undefined method `model_name' for Array:Class
I think I am doing sth wrong with the partial view, Thanks you in advance :)

In your view you have:
<ul >
<%= render :partial => "post", :locals => {:review => #reviews} %>
</ul>
You're passing #reviews here which is an array, not the model #review. Delete the s at the end of #reviews here and it should work. :)
EDIT: If you want to render a list of all the reviews in #reviews, you could do something like this:
<ul >
<%= render :partial => "post", :collection => #reviews %>
</ul>
Check the documentation on partials for more options on how to do this.

Related

link_to expecting keyword_end rails

i don't understand why but i still have a problem when following the ruby on rails tutorial. It's when you put the links on to navigate between the different page..
this is my view:
<h1>New Article</h1>
<%= form_for #article, url: articles_path do |f| %> <!<%= form_for :article, url: articles_path do |f| %> >
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<%= link_to 'Back', articles_path %>
and this is my controller
class ArticlesController < ApplicationController
def new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new' #on utilise render car cela permet de recharger la requête d'article
#qui vient d'échouer contrairement à redirect_to qui lancerais une nouvelle requete
end
end
def index
#articles = Article.all
end
def edit
end
def show
#article = Article.find(params[:id])
end
def update
end
def destroy
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
if you need some over tell me.
So if you have any tips, thanks a lot
Its not the link that is expecting end keyword. The actual error is due to no end tag to the form.
Please update yours as below
<h1>New Article</h1>
<%= form_for #article, url: articles_path do |f| %> <!<%= form_for :article, url: articles_path do |f| %> >
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<% #article.errors.full_messages.each do |msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<%end%>
<%= link_to 'Back', articles_path %>
It seems you have not initialized your #article variable in the new action.
In your new action write it as #article = Article.newand it should work fine.
As you have not initialized #article, it gives you a nil value error in the form.
Thanks for your quick answer, and i did what you told me to do. In fact I missed the end in the form. But now a new error comes which points the "heading" line 2:
Error message:
"First argument in form cannot contain nil or be empty"
The new code
<h1>New Article</h1>
<%= form_for #article url: articles_path do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<%end%>
<%= link_to 'Back', articles_path %>
Can you find the bug. I would like to improve myself in Ruby and Ror.
Thanks a lot

How to write partial ERB template in Sinatra? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How to refactor this code? I write on Sinatra I need to separate into a separate file
<div class="row">
<div class="col-xs-3">
<% #user.posts.each do |post| %>
<%= post.title %><br>
<p><%= post.body %></p>
<% unless post.comment.blank? %>
<% post.comment.each do |comment| %>
<p><%= comment.body %></p>
<% unless comment.comment.blank? %>
<% comment.comment.each do |comment2| %>
<p><%= comment2.body %></p>
<% unless comment2.comment.blank? %>
<% comment2.comment.each do |comment3| %>
<p><%= comment3.body %></p>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
</div>
my model looped comment
def up
create_table :comments do |t|
t.string :body
t.integer :post_id
t.integer :comment_id
t.timestamps
end
end
This is my migration model comment
From the documentation:
Use it as follows to render the mypartial.haml(1) or the
admin/mypartial.haml(2) partials, or with a collection (3) & (4):
<%= partial(:mypartial) %> <!--(1)-->
<%= partial(:'admin/mypartial') %> <!--(2)-->
<%= partial(:object, :collection => #objects) %> <!--(3)-->
<%= partial(:'admin/object', :collection => #objects) %> <!--(4)-->
In (1) & (2), the partial will be rendered plain from their files,
with no local variables (specify them with a hash passed into
:locals). In (3) & (4), the partials will be rendered, populating the
local variable object with each of the objects from the collection.
So your code should be:
<div class="row">
<div class="col-xs-3">
<% #user.posts.each do |post| %>
<%= post.title %><br>
<p><%= post.body %></p>
<%= partial(:comment, :collection => post.comment) %>
<% end %>
</div>
</div>
with the new file comment.erb:
<p><%= comment.body %></p>
<%= partial(:comment, :collection => comment.comment) %>

How to get one model value from another model in ruby on rails?

I got a task to integrate multiple models in a single form.I have one form 'register' and two models buyer and address. But by doing this i can not attach two forms together.
_form.html.erb is
<% #register.buyers.build %>
<%= form_for(#register) do |f| %>
<% if #register.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#register.errors.count, "error") %> prohibited this register from being saved:</h2>
<ul>
<% #register.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<div class="field">
<h4>Buyer</h4>
</div>
<div class="field">
<%# f.fields_for :buyers do |builder| %>
<%= render :partial => "buyer_fields", :locals => {:f => f } %>
<%# end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
_buyer_fields.html.erb is
<% f.fields_for :buyers do |buyers_form| %>
<div class="fields">
<p>
<%= buyers_form.label :name, "Name" %><br/>
<%= buyers_form.text_field :name %>
</p>
<h4>Address</h4>
<% f.fields_for :addresses do |builder| %>
<%= render :partial => 'address_fields', :locals => { :f => builder} %>
<% end %>
</div>
<% end%>
and the _address_fields.html.erb is
<p class="fields">
<table>
<tr>
<td>
<%= f.text_area :name, :rows => "2",:cols => "20" %>
</td>
</tr>
</table>
</p>
register model is
class Register < ActiveRecord::Base
attr_accessible :date, :book_ids,:buyers_attributes
has_many :authorships
has_many :books, :through => :authorships
has_many :buyers
#accepts_nested_attributes_for :buyers, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
accepts_nested_attributes_for :buyers, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
buyer model is
class Buyer < ActiveRecord::Base
belongs_to :register
attr_accessible :addresses_attributes, :name
has_many :addresses, :dependent => :destroy
accepts_nested_attributes_for :addresses, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end
and address model is
attr_accessible :name
belongs_to :buyer
But only register form is displayed. How can i integrate two models in single form in ruby on rails 3.2.9? Please help.
Your nested address view is wrong.
Make some changes in buyer_fields.html.erb
<%= f.fields_for :buyers do |buyers_form| %>
<div class="fields">
<p>
<%= buyers_form.label :name, "Name" %><br/>
<%= buyers_form.text_field :name %>
</p>
<h4>Address</h4>
<%= buyers_form.fields_for :addresses do |builder| %>
<%= render :partial => 'address_fields', :locals => { :f => builder} %>
<% end %>
</div>
<% end %>

Fields_for Nested Model Rails 3.2.2

I have some nested models in my Rails application.
i have an article hat has mny properties.
class Article < ActiveRecord::Base
has_many :properties, :dependent => :destroy
accepts_nested_attributes_for :properties
end
class Property < ActiveRecord::Base
belongs_to :article
end
And now i want to edit this in my View so I editet the controler
# GET /articles/new
# GET /articles/new.json
def new
#article = Article.new
3.times { #article.properties.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: #article }
end
end
And also edited the View and the _format.html.erb
<%= form_for(#article) do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<% f.fields_for :properties do |prop| %>
<div class="field">
<%= prop.label :name %><br />
<%= prop.text_field :name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
But there is no way to show up. If i want to create a new Model, i can not see any input fields for the properties.
What have i do wrong?
You're missing an = in your fields_for line. That is, it should be:
<%= f.fields_for :properties do |prop| %>

Unable to use paperclip with rails 3.1

I'm trying to use paperclip with rails 3.1 but I keep getting this routing error.
No route matches {:action=>"show", :controller=>"users"}
I followed the instructions at thoughtbot/paperclip on github.
<%= form_for :user, #user, :url => user_path, :html => { :multipart => true } do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :FirstName %><br />
<%= f.text_field :FirstName %>
</div>
<div>
<%= f.label :avatar%>
<%= f.file_field :avatar %>
</div>
<div class="field">
<%= f.label :LastName %><br />
<%= f.text_field :LastName %>
</div>
<div class="field">
<%= f.label :Email %><br />
<%= f.text_field :Email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
User.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
routes.rb
City::Application.routes.draw do
resources :users
end
show.html.erb
<p id="notice">
<%= notice %>
</p>
<p>
<b>Firstname:</b>
<%= #user.FirstName %>
</p>
<p>
<b>Lastname:</b>
<%= #user.LastName %>
</p>
<p>
<b>Email:</b>
<%= #user.Email %>
</p>
<p>
<b>Avatar</b>
<%= image_tag #user.avatar.url %>
<%= image_tag #user.avatar.url(:medium) %>
<%= image_tag #user.avatar.url(:thumb) %>
</p>
<%= link_to 'Edit', edit_user_path(#user) %> |
<%= link_to 'Back', users_path %>
!UPDATE!
$ rake routes
events GET /events(.:format) {:action=>"index", :controller=>"events"}
POST /events(.:format) {:action=>"create", :controller=>"events"} new_event GET
/events/new(.:format) {:action=>"new",
:controller=>"events"} edit_event GET /events/:id/edit(.:format)
{:action=>"edit", :controller=>"events"}
event GET /events/:id(.:format) {:action=>"show", :controller=>"events"}
PUT /events/:id(.:format) {:action=>"update", :controller=>"events"}
DELETE /events/:id(.:format) {:action=>"destroy", :controller=>"events"}
root / {:controller=>"events", :action=>"index"}
/:controller(/:action(/:id(.:format)))
I believe you should use users_path (plural) and, the user routes are not in your rake output, can you check your routes file?
The error you are having is not from paperclip cos paperclip has nothing to do with that error message you are having. check your routes very well and also try to restart you sever.

Resources