I have a simple partial to show some topics from the associated community
<%= render :partial => 'shared/topic', :collection => #community.topics %>
I'm trying to make a mobile version of the site, and to not render the partial to the same view, but to a new view.
I tried something like this
def topicsCommunity
fetch_topics ["community_id = ?", #community.id]
render :action => 'index'
end
But I can't get the community.id from my community view.
Also tried this :
#topicscommunity = #community.topics.find(:all,
:conditions => {:community_id => #community.id})
But from the topics_Controller, it didn't work.
Thanks for the help.
You don't have to use render :partial => ... in views only. You can easily do it in your controller (instead of render :action => ... or whatever).
So, just put this in the end of your controller
render :partial => 'shared/topic', :collection => #community.topics
There's no fundamental difference between calling render with :action, :partial, :text, :template or any other hash key.
If you just want to render the same template, use:
def topicsCommunity
fetch_skills ["community_id = ?", #community.id]
render 'index'
end
Related
I want to render a few instances of Post object on a single page.
Currently I have in my controller the code:
#post = Post.find(1)
and in the index view I have:
<%= render :partial => 'posts/post', :object => Post.find(1) %>
Now I want to print the second instance, how to do it? This code for the second instance does not work.
<%= render :partial => 'posts/post', :object => Post.find(2) %>
Couple of things:
When you set instance variable (prefixed with #) in your controller, they're available in your views. So you could do something like:
<%= render :partial => 'posts/post', :object => #post %>
Generally, index actions are meant to deal with a collection (#posts). I'd expect something like:
#posts = Post.where(author: current_user)
If you want to display a post identified by an id, I'd recommend taking a look at CRUD operations: https://guides.rubyonrails.org/routing.html#resources-on-the-web
Roughly, what you may want to do is something like:
# config/routes.rb
resources :posts
# app/controllers/posts_controller.rb
def show
#post = Post.find(params[:id])
end
# app/views/posts/show.html.erb
<%= render partial: 'posts/post', locals: { post: #post } %>
Remember that controllers sets the instance variables, and views use them
I have a 2.3.11 Rails app, and I would like to add a fulltext search to be able to search on the 'articles' content and title. I am using Thinking-sphinx for this purpose. I just want to be able to click on the search link_to '/advanced_search' (on the home page) and have the search form come up, then once the search form is submitted have another view search_results.html.erb display a list of matching article(s).
Problem: I found out that I really don't understand how views are actually rendered. I'm also not sure I got my routing correct. I've tried different things, but for some reason all the examples I found out there are different.
So I have a search form /controllers/articles_controller/advanced_search :
<% form_tag :controller => 'articles', :action => 'search', :method => 'get' do %>
<%= text_field_tag :search, params[:search], :id => 'search_field' %>
<%= submit_tag 'Search', :name => nil %>
<% end %>
And I have a second view to display the search results, /controllers/articles_controller/search_results.html.erb:
<% #articles.each do |article| %>
<li><%= link_to article.name, :action => 'show', :id => article.id %></li>
<li><%= article.archived? %></li>
<% end %>
This my model where I define the index for sphinx:
class Article < ActiveRecord::Base
validates_presence_of :name
#rest of validation and associations here
define_index do
indexes summary
indexes :name
end
end
This my controller and new search action /controllers/articles_controller:
class ArticlesController < ApplicationController
load_and_authorize_resource
def index
#articles = Article.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #articles }
format.json { render :json => #articles }
end
end
def search
#articles = Article.search params[:search]
respond_to do |format|
format.html #
format.xml { render :xml => #articles }
format.json { render :json => #articles }
end
end
end
In my routes.rb I have this line, which is probably not correct, but when I take it out, I get the routing error 'No route matches "/advance_search" with {method => get}
map.connect 'advance_search', :controller => "articles, :action => search
I would appreciate any ideas on how to have my views render correctly, assuming that everything else is correct.
one of the issues that messed me up was the fact that nested routing was I think over used. I found a way around this problem for now. I would have loved to leave a straight answer for anyone with the same question, but unfortunately, my solution does not answer this particular question since I went about it a completely different way. I did however notice later that there are other similar questions that were in fact answered.
What is the difference between render :action => "new" and render :template => "users/new"? I have heard that rendering template, we can use for views from other controllers. Is that it or is there any difference in rendering layout also between the two? For render :template, is it neccessary to have an action defined or is the view page itself enough?
There is no difference. render :template => 'some/thing' is the same as just render 'some/thing', as well as the same as render :action => 'thing' if we are in the some controller.
From Ruby On Rails guide;
render :edit
render :action => :edit
render 'edit'
render 'edit.html.erb'
render :action => 'edit'
render :action => 'edit.html.erb'
render 'books/edit'
render 'books/edit.html.erb'
render :template => 'books/edit'
render :template => 'books/edit.html.erb'
render '/path/to/rails/app/views/books/edit'
render '/path/to/rails/app/views/books/edit.html.erb'
render :file => '/path/to/rails/app/views/books/edit'
render :file => '/path/to/rails/app/views/books/edit.html.erb'
Previously, calling render "foo/bar" in a controller action was equivalent to render file: "foo/bar". In Rails 4.2, this has been changed to mean render template: "foo/bar" instead. If you need to render a file, please change your code to use the explicit form (render file: "foo/bar") instead.
http://guides.rubyonrails.org/4_2_release_notes.html#render-with-a-string-argument
render :action => 'some_controller_action', :layout => 'some_layout_in_layout_folder'
will render the view of that controller action and apply the asset settings (javascript, CSS) of the some_layout_in_layout_folder layout.
If you write this in a controller action, it will use the styling of the specified layout in conjunction with any layout defined at the beginning of the class, if any
I am running Ruby on Rails 3 and I would like to render a template (show.html.erb) passing a local variable.
In RAILS_ROOT/views/users/show.html.erb I have
Name: <%= #user.name %>
Surname: <%= #user.surname %>
I have also a page controller to handle pages and in the application_controller.rb an istance of #current_user. A page is called user, so in RAILS_ROOT/views/pages/user.html.erb I have
<%= render :template => "users/show", :locals => { :user => #current_user } %>
The above code doesn't work (I get this error: RuntimeError in Pages#user, Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id) but this works:
<%= render :template => "users/show", :locals => { :user => #user = #current_user } %>
I think it is not a good approach to "overwrite" the #user variable. This is because, for example, if I need to recall #user after the above 'render' statement it will don't work anymore.
So, what is a solution in order to render show.html.erb?
I tryed also
<%= render :template => "users/show", :locals => { #user => #current_user } %>
<%= render :template => "users/show", :locals => { :object => #current_user, :as => #user }
but those don't work.
UPDATE
If in pages_controller.rb I put this
def user
#user ||= #current_user
end
it will work and in the view files you can just use
<%= render :template => "users/show" %>
Anyway, I discoverd that I have this error (see below for more info):
ActionController::RoutingError in Pages#user
No route matches {:action=>"destroy", :controller=>"users"}
The error is generated from this form statement located in a partial loaded from show.html.erb:
<%= form_for(#user, :url => user_path) do |f| %>
...
<% end %>
:locals => { :user => #current_user }
and in template
Name: <%= user.name %>
Local variables are local, so you don't need # to refer them.
#user502052
You can render view explicitly from your controller.
render :template => "users/show", :locals => {...}
When you don't execute render in controller, framework does that for you with default parameters. When you do, you can specify different template file, pass local variables, render a partial: anything render function supports.
Just in case you are NOT rendering a partial, and do not want to use :template option, this can also be done in Rails 4 with:
render 'users/show', {user: #current_user}
Url:
http://localhost/pages/user
this will call the user method on pages_controller. You have this:
def user
#user ||= #current_user
end
Rails creates an instance variable #users and by default will try to render a view template at app/views/pages/user.html.erb. If that is not what you want, you have to say so in the controller:
def user
#user ||= #current_user
render :template => "users/show", :locals => { :user => #current_user }
end
This will now render app/views/users/show.html.erb with a local variable called user instead of the default app/views/pages/user.html.erb.
Currently you are waiting until you are inside a view template and then asking to render another view template. Once you are in a view template you should only need to render partial templates:
render :partial => 'users/show', :locals => {:user => #current_user}
Hopefully that helps clarify.
I am working through a tutorial with the following code:
<h3>New Comment</h3>
<%= render :partial => #comment = Comment.new,
:locals => { :button_name => "Create" } %>
I believe that 'render :partial => #comment' works like 'render :partial => "comment", :object => #comment'
Where does ' = Comment.new' fit in?
Is it shorthand for :object?
Alan
In Ruby terms,
#obj = Object.new # returns #obj
So you're rendering a comment partial and creating a new comment object that it can work with at the same time.
See http://apidock.com/rails/ActionView/Partials section "Rendering objects with the RecordIdentifier":
# <%= render :partial => "accounts/account", :locals => { :account => #buyer } %>
<%= render :partial => #account %>
Though documented, this is hardly used. The new+assignation (as explained by aharon) works, but it seems a bit tricky. In a tutorial you would expect to find a more orthodox approach:
Create objects in controllers not in views.
Use render :partial => 'mypartial', :locals => {...}