Sorry for a simple question but i am a bit confused trying to follow ruby on rails tutorial book
I am at the chapter 10 and confused, yes i trick a bit my version for learning purpose
So I have a controller called customer
customers_controller.rb
def show
#customer = Customer.find(params[:id])
#posts = #customer.posts
end
I then have the following folder
_post.html.erb
Welcome to a post
Which his called from the show customer file has follow
/view/customer/show.html.erb
<% provide(:title, #customer.name) %>
<aside class="customer_show_nav">
<h1><%= #customer.name %></h1>
<%= #customer.email %>
</aside>
<div class="events">
<%= render #posts %>
</div>
But when loading nothing his appearing not even Welcome to a post. What i am doing wrong?
Thanks in advance. I am following the tutorial http://ruby.railstutorial.org/chapters/user-microposts#top 10.22
if _post.html.erb is in view/customers
you do this in view/customers/show.html.erb
<%= render 'controller_where_post_lives/post' %> which will look for customers/_post.html.erb
Sometimes, you also need = in <%= %> with rails 3
Also, show is used to show one item, and index is used to show all items.
So to render you will do
<%= render :partial => "post", :collection => #posts %>
Edit:
When you call render you give it the view, no objects here. Unless like above passing a collection.
http://guides.rubyonrails.org/layouts_and_rendering.html
Also if you just want to render text you can do render :text => "OK"
Look for partials explanation.
Related
I have a Users controller, with index, show, edit and destroy methods. In my layouts/ folder, I have a general-purpose user.html.erb layout that renders some partials. These partials are of course producing errors due some of the info isn't available, like #user.name, for example. I've tried to render that partial always when I'm in a def show state, something like:
<% if Users.show %>
<% render "shared/asides/users" %>
<% else %>
Other partials
<% end %>
I've tried several ways and I always get errors. I feel totally lost even trying to find out this on the Rails documentation nothing seems to be indicated there too.
Your problem is, as you say, you're trying to display things associated with a user, like #user.name, but there is no #user.
So why not check for #user before showing the partial? Or if you have a collection of users, I'm guessing #users?
<% if #users %>
<%= render "shared/asides/users" %>
<% else %>
<%= Do something else %>
<% end %>
Of maybe a bit neater:
<%= render (#users ? path/to/partial_a.html.erb : path/to/partial_b.html.erb) %>
You can make a special layout for your action. Then, at the end of action add layout to render.
def show
...
render layout: "custom_layout"
end
In my app/views/conversations/index.html.erb, I am writing:
<%= render #conversations %>
hoping that it would find a partial named _conversation.html.erb inside the same directory, and use it to render each elements in #conversations. (The usual Rails way)
But I get a missing template error: Missing partial mailboxer/conversations/_conversation.
I am using a Mailboxer gem, and there were no documentations for this. I know I could render a partial explicitly by <%= render partial: 'conversation', locals: { conversations: #conversations } %>.
Yet still, I would like to know why my app is looking for a partial for #conversations in mailboxer/conversations/, not conversations/, and if there is a way to change this behavior.
More information
<% #conversations.each do |conversation| %>
<%= div_for conversation %>
<% end %>
produces HTML:
<div class="mailboxer_conversation" id="mailboxer_conversation_16"> ... </div>
<div class="mailboxer_conversation" id="mailboxer_conversation_17"> ... </div>
....
Perhaps the mailboxer_ in front of conversation has something to do with this situation also?
This happens because, in later versions of Mailboxer, models are namespaced under Mailboxer. (e.g. Mailboxer::Conversation, Mailboxer::Message.)
I commented on the GitHub issue also.
You could try providing the full path to the partial, e.g.
<%= render :partial => "yourfoldername/conversation", collection: #conversations %>
I'm new to ruby on rails.
In views/events I have "_form.html.erb" which is rendered in "new.html.erb" by this code:
<%= render "form" %>
Now I want to render "_form.html.erb" in "index.html.erb" which is in the same folder(views/events).
But I get the error "missing template".
I guess I have to add some thing to controller, please help me to render form in other pages of views...
You "usually" don't render a form in an index action. Most form partials are setup semantically to expect a #my_resource, but if you're doing everything the rails way you're not going to have a instance variable during your index action. There's a number of ways you can do this but this is probably the quickest.
You probably have some collection (let's pretend you're using books) in your index action:
#views/books/index.html.erb
<% #books.each do |book| %>
...
<%= render "form" %>
...
<% end %>
You can just set an instance variable somewhere prior to rendering the form:
#views/books/index.html.erb
<% #books.each do |book| %>
<% #book = book %>
...
<%= render "form" %>
...
<% end %>
Another way to do it would be through passing in some locals to a partial. You'd have to change all of your references in _form to use a local variable instead. Then you can call render like this:
<%= render :partial => "form", :locals => {:book => book } %>
You can try
<%= render "events/form" %>
I had this problem before and this solved
I'm quite sure this is a silly error but I'm unable to spot it. Please help me out on this.
I'm using a select tag to select a topic, which triggers the filter_by_content method. I'm using Rails 2.3.2.
This is my controller code
def filter_by_content
#articles = Article.find(:all)
end
My RJS (filter_by_content.rjs)
update_page do |page|
page.replace_html 'articles', :partial => 'main/filtered', :object => #articles
end
My Partial 'filtered'
<div id = "articles">
<% if #articles %>
<% #articles.each do |article| %>
<%= article.title %>
<% end %>
<% end %>
</div>
I checked my server, and the articles are sure getting fetched but the problem is with displaying them.
Ok turns out, if I remove the reference to JQuery and use only prototype, it works fine.
So I had to install JRails and now it works fine.
I'm new to rails and thought I had finally figured out some of this routing stuff, but have been going in circles with this bit all day.
I was following a tutorial about building a twitter like service, and I've got the basics working from the tutorial, but with Mongo instead of mySql.
I've got 3 types of pages.
The home page which is showing all the posts ordered by date
The user page which is showing the posts from a specific user
The posts page which is showing posts from a users friends.
So for each page, I've done the following
1) created a method in the corresponding controller to get the correct posts
2) created a _posts.html.erb page with the display parameters, which are slightly different on each page
3) referenced the partial in the index.html.erb page for each view.
The controller entries look like this
def index
#posts = Post.all(:order => 'created_at DESC')
end
or
def posts
#posts = Post.all(:conditions => {'user_id' => params[:id]}, :order => 'created_at DESC')
end
and the partials are
<%= render :partial => #posts %>
In each view is a _posts.html.erb file, and each is slightly different
home/_posts.html.erb looks like this
<%= div_for post do %>
Posted <%= time_ago_in_words(post.created_at) %> ago
Posted By <%= post.user_id %>
<%= post.text %>
<% end %>
while posts/_post.html.erb looks like this
<%= div_for post do %>
Posted By <%= post.user_id %>
<%= post.text %>
<% if post.created_at > 52.hours.since %>
<%= distance_of_time_in_words_to_now(post.created_at) %>
<% else %>
<%= post.created_at.strftime("%c") %>
<% end %>
<% end %>
Now the strange part is that on all the pages index.html.erb, users/show.html.erb, posts/index.html.erb, the partial that is being displayed is the posts/_post.html.erb. The others are being completely ignored.
my understanding was that render :partial would take the #posts and render _posts.html.erb from the current view. But this isn't happening, as only the posts/_post.html.erb is being rendered from all views.
I've looked in the routes.rb file, but don't have anything in there that would cause this problem.
Can anybody tell me why I am not displaying the proper partials?
-----------Edited --------------------------------
The directory structure for views is as follows
views
- home
-_post.html.erb
-index.htlm.erb
- layouts
- posts
-_post.html.erb
-index.html.erb
-posts.html.erb
- sessions
- users
-_post.html.erb
-new.html.erb
-show.html.erb
I hope that helps.
"post", :collection => #posts%>
maybe rails automatically defines path to the partial when you pass only collection
You're passing the collection as the argument that rails is expecting to be the name of the partial. Your call to render should look like this
<%= render partial: "post", collection: #posts %>
This will render app/views/posts/_post.html.erb, passing the local variable post to the partial.
Additionally, (is sometimes handy) there's an iteration object that is made available to this view, partial_name_iteration, that has information about the total size of the #posts collection, and the index of the current object.