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
Related
I am trying to render a templated under
public/index.html.erb
def public_index
#post = Post.all
render 'public/index'
end
Missing template public/index
I don't see anything wrong with it. Is public a reserved word or something?
Try using:
render :file => "#{RAILS_ROOT}/public/index.html.erb"
Try
render :template => 'public/index'
If this will not help then try following ways
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"
Check http://guides.rubyonrails.org/layouts_and_rendering.html
You can do like this:
Add this, into your routes.rb file.
match '/index', :to => redirect('/index.html')
Update
In Rails 4, it should use "get", not "match":
get '/index', :to => redirect('/index.html')
I have read multiple tutorials where a partial is rendered as render #products, and then if you ajax a form for a new product, in the .js file you can do a render #product render the new product on the page.
However this only works if the view folder you are working in is called products, else you have to render the partial via render :partial => 'product', :collection => #products. Then when I want to ajax a form, I am unable to just append the new product with render :partial => 'product', :collection => #product. I have to clear the div and render all of the products again.
Is there a way to just render the new product, rather than having to clear and re-render all of the products when not working in a view folder with the same name as the model?
So in my controller, I had to add
class PostController < ApplicationController
respond_to :html, :js
then
def add
#post = Post.new(params[:post])
if #post.save
respond_with(#post, request.referer)
Then I had to make sure I had add.js.erb in my app/views/post, and in the js render the partial like render :partial => 'public/post, :object => #post
Not sure why I'm getting this. I did a bunch of reading and I can't make heads or tails of this.
My controller:
def create
#emails = Email.new(params[:email])
respond_to do |format|
if #emails.save
flash[:notice] = 'Email was successfully created.'
format.html { redirect_to admin_emails_path(:mail_type => #emails.mail_type) }
format.xml { render :xml => #emails, :status => :created, :location => #emails }
else
format.html { render :action => "new" }
format.xml { render :xml => #emails.errors, :status => :unprocessable_entity }
end
end
end
Nothing crazy there. Its a multipart(images) form submission..maybe that has something to do with?
Update
Some irb stuff:
>> admin_emails_path(:mail_type => #emails.mail_type)
"/admin/emails?mail_type=magic_email"
>> admin_emails_path(#emails)
"/admin/emails.%23%3Cemail:0x109eb6360%3E"
The second example seems to be what it actually is returning, ignoring my additional params in the URL.
I should also note that my edit redirect is identical, and it works perfectly.
Update 2
Just to show how completely helpless this situation is, I've changed my controller to this :
if #emails.save
flash[:notice] = 'Email was successfully created.'
debugger
format.html { render :action => "new" } # <=== WTF ?
format.xml { render :xml => #emails, :status => :created, :location => #emails }
else
And I still get this:
Completed in 7401ms (View: 3, DB: 7) | 406 Not Acceptable [http://localhost/admin/emails.%23%3Cemail:0x109fd2a28%3E]
Routes
admin.resources :emails, :collection => {:test_email => :get}, :member => {:update_current => :get, :send_email => :get, :duplicate => :get} do |email|
email.resources :distributions, :collection => {:delete_dist => :get}
end
Form
- form_for #emails, :url => admin_email_path(#emails), :id => "email_form", :html => {:multipart => true} do |f|
... lots of stuff ..
.clear
%p
= f.submit 'Save Email', :class => "button"
The MIME type for the request is determined by the file extension incoming.
The error here is the following line:
>> admin_emails_path(#emails)
"/admin/emails.%23%3Cemail:0x109eb6360%3E"
The helper admin_emails_path should not be passed the list of e-mails. This collection path should work on it's own. When you pass in the #emails object, it's trying to encode it into the URL and injecting a period, which rails is parsing like a file extension (the url decoded version of %23%3Cemail:0x109eb6360%3E).
Change the reference from:
admin_emails_path(#emails)
to:
admin_emails_path
...and you will not see these format errors.
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
I am building a fairly simple recipe app to learn RoR, and I am attempting to allow a user to save a recipe by clicking a link rather than through a form, so I am connecting the user_recipe controllers 'create' function through a link_to.
Unfortunately, for some reason the link_to is calling the index function rather than the create.
I've written the link_to as
<%= "save this recipe", :action => 'create', :recipe_id => #recipe %>
this link is on the user_recipes/index.html.erb and is calling the 'create' function of the same controller. It doesn't seem to make a difference if I include the :controller or not.
The controllers look like this
def index
#recipe = params[:recipe_id]
#user_recipes = UserRecipes.all # change to find when more than one user in db
respond_to do |format|
format.html #index.html.erb
format.xml { render :xml => #recipes }
end
end
def create
#user_recipe = UserRecipe.new
#user_recipe.recipe_id = params[:recipe_id]
#user_recipe.user_id = current_user
respond_to do |format|
if #menu_recipe.save
format.html { redirect_to(r, :notice => 'Menu was successfully created.') }
format.xml { render :xml => #menu, :status => :created, :location => #menu }
else
format.html { render :action => "new" }
format.xml { render :xml => #menu.errors, :status => :unprocessable_entity }
end
end
In the standard REST scheme the index action and the create action both have the same url (/recipes) and only differ in that index is accessed using GET and create is accessed using POST. So link_to :action => :create will simply generate a link to /recipes which will cause the browser to perform a GET request for /recipes when clicked and thus invoke the index action.
To invoke the create action use link_to {:action => :create}, :method => :post, telling link_to explicitly that you want a post request, or use a form with a submit button rather than a link.
Assuming you have default resources set up in your routes file, i.e. something like this
resources :recipes
The following will generate a link that will create a recipe; i.e. will be routed to the create action.
<%= link_to "Create Recipe", recipes_path, :method => :post %>
For this to work, JS needs to be enabled in your browser.
The following will generate a link that will show all recipes; i.e. will be routed to the index action.
<%= link_to "All Recipes", recipes_path %>
This assumes the default which is a Get HTTP request.