Where should I place my respond_to block? - ruby-on-rails

Lets say I have an ArtcilesController with create action like following.
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to(#article, :notice => "Article created") }
format.json { render :show }
else
format.html { render :new }
format.json { render(:json => { :errors => #article.errors }, :status => :not_acceptable) }
end
end
end
The same action can be written like following also:
def create
#article = Article.new(params[:article])
if #article.save
respond_to do |format|
format.html { redirect_to(#article, :notice => "Article created") }
format.json { render :show }
end
else
respond_to do |format|
format.html { render :new }
format.json { render(:json => { :errors => #article.errors }, :status => :not_acceptable) }
end
end
end
Notice that in first example there is an if else block inside of a respond_to block and in second, there are two respond_to blocks inside of a single if else block.
Should I prefer one over other? If yes, any reasons why? Or is it just a matter of choosing a style and sticking with it?

Style only however you're only ever responding to one request and using routing logic in your controller according to your models.
def create
#article = Article.new(params[:article])
respond_to do |format|
format.html {
#article.save ? redirect_to(#article, :notice => "Article created") : render :new
}
format.json {
#article.save ? render(:show) : render(:json => { :errors => #article.errors }, :status => :not_acceptable)
}
end
end

respond_with in Rails 3 will DRY this up, for boilerplate code as above. It even handles rendering edit/new forms and error messages when validation fails.

Related

Ruby on Rails, :notice

I am using this file uploader example for Ruby on Rail.
I have this piece of code in my controller. And I need to have a :notice parameter somewhere, so when the file is uploaded the notice will be "You have uploaded a file", if there is a error then "Something went wrong"
def create
p_attr=params[:upload]
p_attr[:arraydb] = params[:upload][:upload].first if params[:upload][:upload].class == Array
#upload = Upload.new(p_attr)
respond_to do |format|
if #upload.save
#upload.update_attributes(:user_id => current_user.id)
format.html {
render :json => [#upload.to_jq_upload].to_json,
:layout => false
}
format.json { render json: [#upload.to_jq_upload].to_json, status: :created, location: #upload }
else
format.html { render action 'new' }
format.json{ render json: {name:(#upload.upload_file_name).split(".").first ,error: #upload.errors.messages[:upload_file_name]}, :status =>422}
end
end
end
So, I need something like this:
format.html { redirect_to(#upload, :notice => "LALALALALALA") }
but I have no idea how to integrate the :notice into my code
Thanks in advance.
This is how you 'integrate' the notice to your responses
def create
p_attr=params[:upload]
p_attr[:arraydb] = params[:upload][:upload].first if params[:upload][:upload].class == Array
#upload = Upload.new(p_attr)
respond_to do |format|
if #upload.save
#upload.update_attributes(:user_id => current_user.id)
format.html { redirect_to(#upload, :notice => "Success") }
format.json { render json: [#upload.to_jq_upload].to_json, status: :created, location: #upload }
else
format.html { render action 'new', :notice => "Failed" }
format.json{ render json: {name:(#upload.upload_file_name).split(".").first ,error: #upload.errors.messages[:upload_file_name]}, :status =>422}
end
end
end
When you say 'integrate', do you mean how I can use the value of notice in view or the controller method?
If so, you can just use params[:notice] to get the value in your view or the controller you are redirecting to.

Rails 3 - Redirect_to / Render another controller

I have 2 controllers: Projects and Users. Both models have no relation at all.
When I create a new Project I want to redirect to the new User path after saving the new project, but all my tries give erros like missing template or stuff like that.
How can I get this working?
EDITED
My create method in Projects controller:
def create
#project = Project.new(params[:project])
respond_to do |format|
if #project.save
format.html { render (new_user_registration_path) }
else
format.html { render :action => "new" }
format.xml { render :xml => #project.errors, :status => :unprocessable_entity }
end
end
end
You don't want to render new_user_registration_path, you want to redirect_to new_user_registration_path
you must use redirect_to instead render:
redirect_to new_user_registration_path
respond_to do |format|
if #project.save
format.html { redirect_to new_user_registration_path }
else
format.html { render :action => "new" }
format.xml { render :xml => #project.errors, :status => :unprocessable_entity }
end
end

The action could not be found for controller

Having gone through my code I have a separate problem from my original question and rather than writing a new question. I will leave the old part at the bottom of this and post the new problem here. I do this because they are closely related.
New:
Im getting an error message saying
Unknown action
The action 'response' could not be found for CrawlerController
I'll keep it simple but the code for model, controller and routes are below in the previous question.
A basic run down is response is a def within CrawlerController as is add_Request.
The routes are matched as such:
match "/requests/new" => "crawler#add_Request"
match 'requests/:id' => 'crawler#response'
Here is controller code as per user request:
class CrawlerController < ApplicationController
def add_Request
#request = Request.new(params[:request])
respond_to do |format|
if #request.save
format.html { redirect_to(#request, :notice => 'Request was successfully created.') }
format.xml { render :xml => #request, :status => :created, :location => #request }
else
format.html { render :action => "new" }
format.xml { render :xml => #request.errors, :status => :unprocessable_entity }
end
end
end
def response
#request = Request.find(params[:id])
respond_to do |format|
format.html
format.js { render :json => #request }
end
end
def show
#request = Request.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #request }
format.json{
render :json => #request.to_json
}
end
end
end
please recheck code of controller as I can see it
class CrawlerController < ApplicationController
def add_Request
#request = Request.new(params[:request])
respond_to do |format|
if #request.save
format.html { redirect_to(#request, :notice => 'Request was successfully created.') }
format.xml { render :xml => #request, :status => :created, :location => #request }
else
format.html { render :action => "new" }
format.xml { render :xml => #request.errors, :status => :unprocessable_entity }
end
end
def response
#request = Request.find(params[:id])
respond_to do |format|
format.json {render :#request.to_json}
end
end
so one end is missing an your response action is defined inside add_Request

How to create a new object in Rails with a predefined property

I have a Rails app that has a bunch of pages, each page has many convos. On each page there's a link to create a new convo on that page. This is the code for that link:
<%= link_to 'New Convo', new_convo_path(:page=>#page) %>
However, on the next page, "convo/new" the page property is empty. What am I missing?
EDIT here are my new and create functions for convos
def new
#convo = Convo.new(params[:page])
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #convo }
end
end
# POST /convos
# POST /convos.xml
def create
#convo = Convo.new(params[:convo])
respond_to do |format|
if #convo.save
format.html { redirect_to(#convo, :notice => 'Convo was successfully created.') }
format.xml { render :xml => #convo, :status => :created, :location => #convo }
else
format.html { render :action => "new" }
format.xml { render :xml => #convo.errors, :status => :unprocessable_entity }
end
end
end
You need to load the page ... try to set a before filter ...
before_filter :find_page
private
def find_page
#page = Page.find(params[:page_id])
end
Then you build using nested resources
def new
#convo = #page.convos.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #convo }
end
end
def create
#convo = #page.convos.build(params[:convo])
.....
end
My guess is that you are missing a ":page=>"
def new
#convo = Convo.new(:page=>params[:page])
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #convo }
end
end

Missing templates from new Rails 3 app?

Just tried writing a simple validates_presence_of in my model, and when the errors try to render, it calls this :
Template is missing
Missing template posts/create with {:locale=>[:en, :en], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml, :haml], :formats=>[:html]} in view paths "/Users/johnsmith/Sites/shwagr/app/views"
Errors don't have seperate views in Rails3 do they? I thought that was Rails magic..
Curious if anyone had this problem, or knew how to make it correctly validate.
My Model:
validates_presence_of :category, :name, :url
My Controller:
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
def create
#post = Post.new(params[:post])
if #post.valid? && current_user.posts << #post
respond_to do |format|
if #post.save
format.html { redirect_to(#post, :notice => 'Post was successfully created.') }
format.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
end
Update
Interesting, I 'touch app/views/posts/create.html.haml', and now it removed the error and instead loads that page instead. But why would it? Or more importantly, how can I make it just redirect back to the new_post_path(#post) like it should?
If your
if #post.valid? && current_user.posts << #post
line returns false, no render() or redirect_to() is called. Rails' default behavior then is to render the view with the same name as your method. That would be create.BUILDER.FORMAT.
Try to remove the line. Use this code instead:
#post = current_user.posts.new(params[:post])
respond_to do |format|
if #post.save
...
Or write an else case with
render :action => "new"
Ah got it. This is because it was never valid so it would loop back to itself on 'create', find no template there and error out. The correct way to set up the def create would be this
def create
#post = Post.new(params[:post])
if #post.valid? && current_user.posts << #post
respond_to do |format|
if #post.save
format.html { redirect_to(#post, :notice => 'Post was successfully created.') }
format.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { redirect_to new_user_post_path(:current) }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
else
render :action => 'new'
end
end
No, they don't have seperate views. So do you have a app/views/posts/create.html.erb file?

Resources