I want to iterate through an array of objects
<% #users.each do |user| %>
<%= render "member_list" %>
<% end %>
My question is how do I pass the user object to the partial, and how do I reference it in the partial. I know how to do it if it's just a single object, but I don't know how to pass the single object from the array to it.
I tried passing user to it and reference user in the partial, but it doesn't recognize user in the partial.
You might do it without loop - pass it as a :collection parameter. To use a custom local variable name within the partial, specify the :as option in the call to the partial:
<%= render :partial => "member_list", :collection => #users, :as => :member %>
With this change, you can access an instance of the #users collection as the member local variable within the partial.
<% for user in #users %>
<%= render "member_list" , locals => {:user => user}%>
<% end %>
in partial:
Hello. I'm <%= user.name %>
Related
This is likely an error due to my minimal understanding of Rails and how to use variables across models, so if there is more code needed to answer it or if my terminology is incorrect, let me know and I will gladly update the question.
I have a feed of posts that I want a user to be able to "like." While the following code allows likes to work on an individual post's page - site.com:3000/posts/*post.id* - with the form data being passed of like[liked_post_id]:*post.id*, when I try to submit a like on a profile - site.com:3000/users/*user.id* - which contains a feed of posts, the form data being passed is like[liked_post_id]: (blank value)
How can I pass the post's ID within a feed of posts to the liked_post_id variable in _like.html.erb?
I have noticed that the action of the like form is /likes across the board. Would this will only work when you are on the page site.com:3000/posts/*post.id*? I'm curious if I need to modify the it so that the action of the form is /posts/*post.id*/likes when you are on the page site.com:3000/users/*user.id*
From my post view:
#views/posts/_post.html.erb:
...
<%= render 'posts/like_form' if signed_in? %>
...
Route to proper form:
#views/posts/_like_form.html.erb:
<div id="like_form">
<% if current_user.likes_this?(#post) %>
<%= render "posts/unlike" %>
<% else %>
<%= render "posts/like" %>
<% end %>
</div>
Like from:
#views/posts/_like.html.erb
<%= form_for Like.new, :remote => true do |f| %>
<%= f.hidden_field :liked_post_id, :value => #post.id %>
<%= f.submit "Like" %>
<% end %>
From profile (feed of posts):
#views/users/show.html.erb
...
<%= render #posts %>
...
Likes controller:
#controllers/likes_controller.rb
class LikesController < ApplicationController
before_filter :signed_in_user
def create
#post = Post.find(params[:like][:liked_post_id])
current_user.like!(#post)
respond_to do |format|
format.html { redirect_to root_url }
format.js
end
end
...
User model:
#models/user.rb
...
def like!(post)
likes.create!(liked_post_id: post.id)
end
...
#frank-blizzard has pointed out that my form markup is an issue. On a post's page the generated markup is:
<input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" value="73" />
While on the feed page:
<input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" />
You can do something like this:
<% form_for Like.new, :remote => true do |f| %>
<%= f.hidden_field :post_id, :value => #post.id %>
<%= f.submit %>
<% end %>
The current_user.likes.build(...) part should get out of your view and inside your controller. You are using a current_user.like! method so I guess you have implemented already some method in user model to accomplish this. If not build your like in the create action of LikesController where you can access params[:like].
def create
#post = Post.find(params[:like][:post_id])
current_user.likes.build(#post)
# ...
end
EDiT
You might need to pass your #post variable correctly into your _like_form partials, like so:
#views/posts/_post.html.erb:
...
<% if signed_in? %>
<%= render 'posts/like_form', :post => #post %>
<% end %>
...
This will give you acceess to a post variable inside the partial so you can prepopulate your forms value with its id. See this questions as well Pass a variable into a partial, rails 3? and make sure to read up on how to pass variables correctly to partials. you can debug your views using <%= debug <variablename> %>
I am rendering a partial like so:
<% #pages.each do |page| %>
<%= render 'layouts/pagewithchildren', :locals => { :page => page } %>
<% end %>
But when i try to access a variable in page i am getting the error:
undefined local variable or method `page'
I am accessing the variable like:
<%= page.title %>
So what else do I need to do?
i'm not 100% sure but isn't it either
<%= render 'layouts/pagewithchildren', :page => page %>
or
<%= render :partial => 'layouts/pagewithchildren', :locals => { :page => page } %>
?
You have to explicitly specify partial, otherwise, Rails will treat locals as a params hash, you can access locals[:page] but not page variable directly in your partial.
Change your code to:
<%= render partial:'layouts/pagewithchildren', locals: {page: page} %>
I have seen RailsCasts#302 which describes about the in-place editing using the best_in_place gem. Over there in for gender option Ryan uses the array inside the show.html.erb and makes it a dropdown box(see the gender section where he explicitly defines an array).
<p>
<b>Gender:</b>
<%= best_in_place #user, :gender, type: :select, collection: [["Male", "Male"], ["Female", "Female"], ["", "Unspecified"]] %>
</p>
But what I want is I have defined an array inside the Model itself like: (because my array elements are not simple and short in count)
For eg:
user.rb
class User < ActiveRecord::Base
def authencity_types
['Asian', 'Latin-Hispanic', 'Caucasian']
end
end
How am I going to use this array elements as dropdown using the best_in_place syntax.
PS: I did try something like this
<% #users.each do |user| %>
<%= best_in_place user, :authencity, type: :select, :collection => User::authencity_types %>
<% end %>
But it says undefined method authencity_types
You're defining an instance method on the User model, so try this.
<% #users.each do |user| %>
<%= best_in_place user, :authencity, type: :select, :collection => user.authencity_types %>
<% end %>
Alternatively you can define that as a class method like this.
class User < ActiveRecord::Base
def self.authencity_types
['Asian', 'Latin-Hispanic', 'Caucasian']
end
end
Or you may want to consider using a constant if it does not need to be dynamic.
I have one data model 'object' with fields->object_id, object_name.
That is: http://localhost:3000/objects/
I have created another model 'front_pages' (not created any migration in this, instead I have created some pages like 'search.html.erb'(by hand) and the associated controllers).
That is: http://localhost:3000/front_pages/
My question is: How to access/search the items stored in the 'object' database within the 'search.html.erb'.
"These two are in the same rails project folder"
-> How to display the search results into an HTML.erb file?
views/static_pages/show.html.erb
<% #npsobject.each do |npsobjects| %>
Nps:
Nps type:
Nps name:
|
Static_page Controller
class StaticPagesController < ApplicationController
def show
#npsobject=Npsobject.find(:all, :conditions => ['nps_name LIKE ?', "%#{params[ :search]}%"]);
end
views/static_pages/new.html.erb
<%= form_tag( { :action =>"show"}, { :method => "get"}) do %> # The action path is ok??
<%= text_field_tag :search, params[:search], :class => 'inputBox' %>
"button") %>
Please verify the above codes and guide me through, as Im new to RoR..:)
You need to move your
#npsobject = Npsobject.find
into show action
and then each it into your views/static_pages/show.html.erb
<% #npsobject.each do |nps| %>
<%= nps.nps_name %>
<% end %>
Hey,
I'm working on a page having a nested layout. first I have the applicationlayout with my "mainmenu" now I want to add a second menu only on this page. I got this working via
<% render :partial => "mypartial", :layout => 'navigation' %>
this adds my second navigation to the form and renders a partial.
At this point I try to distinguish between two different partials. so my file looks like this
<% if :passed_text == "page1" %>
<%= render :partial => "mypartial1", :layout => 'navigation' %>
<% else %>
<%= render :partial => "mypartial2", :layout => 'navigation' %>
<% end %>
my navigation is as follows:
<%= link_to "Mypartial1", partial_path, :passed_text => :page1 %>
<%= link_to "Mypartial2", partial_path, :passed_text => :page2 %>
<%= yield %>
but it ignores my parameters. I guess I'm missing something basic, but all this is new to me.
thanks for your help
okay I found an answer:
first I have to check for:
params[:passed_text]
instead of :passed_text
secondly passing the parameters has to be in brackets
partial_path( :passed_text => :page1)
this works fine