I need the block respond_to not render to new.html.erb if not a another view created by me called for example new_form.html.erb
def new
#user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #user }
end
end
Pretty simple. As long as the view is in the default directory for the controller:
respond_to do |format|
format.html render 'new'
format.json { render json: #user }
end
If not, you need to tell it which directory:
respond_to do |format|
format.html render 'users/new'
format.json { render json: #user }
end
More docs here: http://guides.rubyonrails.org/layouts_and_rendering.html
There are many ways to do it....
##FOR HTML CALLS
format.html { render 'new'}
format.html { render 'shared/new'}
##FOR JS CALLS
format.js { render 'new'}
format.js { render 'shared/new'}
##pass variable to the view
format.js { render 'shared/new',:locals=>{:type=>"User"}}
##OR you can also try redirect in some rare cases WITHOUT respond_to block
redirect_to users_path(params[:id])
Related
Ok I have two methods
def join_group
#user = User.find(current_user[:id])
#group = Group.find(params[:id])
respond_to do |format|
if #user.groups << #group
format.html {redirect_to #group}
format.js
else
format.html { render :index}
end
end
end
def create
#group = Group.new(group_params)
#group.created_by = current_user.id
respond_to do |format|
if #group.save
format.html { redirect_to #group }
format.js
format.json { render :show, status: :created, location: #group }
else
format.html { render :new }
format.json { render json: #group.errors, status: :unprocessable_entity }
end
end
end
And I want to make that after create action join_group is called. I was trying after_action :join_group, only: [:create] but i don't know how to pass id into join_group and I ended up with ActiveRecord::RecordNotFound (Couldn't find Group with 'id'=):
Instead of a completely separate action to add the user after you create it you could just set it up in the create method.
def create
#adds user to the group and builds it like you have it.
#group = current_user.groups.build(group_params.merge(created_by: current_user.id)
respond_to do |format|
if #group.save
format.html { redirect_to #group }
format.js
format.json { render :show, status: :created, location: #group }
else
format.html { render :new }
format.json { render json: #group.errors, status: :unprocessable_entity }
end
end
end
But if you do want it separate I would add it as a method on the group and just pass the User ID to it.
I would like to render json or html in show. For json format I would like to redner a json object, for html format I would like render show.html.erb view
here is my code
def show
#post = Post.find(params[:id])
respond_to do |format|
format.json { render json: #post }
end
end
It returns an error of
ActionController::UnknownFormat
when I test html format
Thanks for any help
def show
#post = Post.find params[:id]
respond_to do |format|
format.html # index.html.erb
format.xml { render xml: #post}
format.json { render json: #post}
end
end
I'm trying to create two actions that both go to the "new" view. The only difference is I would like the new_e_drawing action to run the incrament_e method, whereas the new action runs the incrament method.
def new
#drawing = Drawing.new
#drawing = #drawing.incrament(#drawing)
respond_to do |format|
format.html # new.html.erb
format.json { render json: #drawing }
end
end
def new_e_drawing
#drawing = Drawing.new
#drawing = #drawing.incrament_e(#drawing)
respond_to do |format|
format.html new.html.erb
format.json { render json: #drawing }
end
end
I would like both of them to take me to the view named "new". I'm not sure how to set up the routing or the respond_to statement for the new_e_drawing action. I tried these with no success:
get 'drawings/new' => 'drawings#new_e_drawing'
match 'drawings/new_e_drawing' => 'drawings#new_e_drawing'
Thanks for the help.
Render the "new" template explicitly in your html block of new_e_drawing action.
def new
#drawing = Drawing.new
#drawing = #drawing.incrament(#drawing)
respond_to do |format|
format.html # new.html.erb
format.json { render json: #drawing }
end
end
def new_e_drawing
#drawing = Drawing.new
#drawing = #drawing.incrament_e(#drawing)
respond_to do |format|
format.html { render :template => "new" }
format.json { render json: #drawing }
end
end
In your routes,
match 'drawings/new_e_drawing' => 'drawings#new_e_drawing'
route for new action will be automatically generated by rails since it is part of CRUD
My controller file looks like this:
class QuotesController < ApplicationController
def show
#quote = Quote.find(params[:id])
#popup = params[:popup]
respond_to do |format|
if #popup.present?
format.html { render layout: false }
else
format.html
end
format.json { render json: #quote }
end
end
def create
#quote = Quote.new(params[:quote])
respond_to do |format|
if #quote.save
format.html { redirect_to #quote, notice: "Quote was successfully created.", popup: "1" }
format.json { render json: #quote, status: :created, location: #quote }
else
format.html { render action: "errors", layout: false }
format.json { render json: #quote.errors, status: :unprocessable_entity }
end
end
end
end
If I visit http://localhost:3000/quotes/1?popup=1 -- the view correctly displays without application_layout
However, if I am coming from the CREATE action, it seems ?popup=1 is never being appended to the URL - and therefore the application_layout is displaying when it should not be
I thought that adding popup: "1" to the redirect_to line was supposed to pass a param via GET
Can anyone see what I am missing?
Thanks
Edit: tried this on my machine and it worked:
{ redirect_to quote_path(#quote, :popup => "1"), notice: "Quote was successfully created." }
Try #quote_url(:popup=>1) I guess it will work.
I want void format.html response in controller in ruby on rails 3.
e.g. in my comments_controller.rb
def new
#comment = Comment.new
respond_to do |format|
#format.html # new.html.erb avoid this output
#format.json { render json: #board } # avoid this output
format.js
end
end
I want only works with format.js response and after, render partial from new.js.erb. This is not problem for me its easy, but...
If I put:
http://localhost:3000/comments/new
I get a blank page :O.
How can I void this page? e.g. render or show a error 404.
Thank you!
You can do something like:
def new
#comment = Comment.new
respond_to do |format|
format.html { render text: "Error", status: 404 }
#format.json { render json: #board } # avoid this output
format.js
end
end
See http://guides.rubyonrails.org/layouts_and_rendering.html for full details.