问题
This is a follow up question to: Rendering different views in one action
The error I am getting is:
Template is missing
Missing template items/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
It should be noted, that I copied the show.html.erb file to two files show_with_edit.html.erb and show_with_star.erb, and deleted show.html.erb to avoid duplicates.
My Code in posts_controller.rb
def show
if signed_in?
show_signed_in
else
show_not_signed_in
end
end
def show_signed_in
#add methods here
@post = Post.find(params[:id])
respond_to do |format|
format.html # show_with_edit.html.erb
format.json { render json: @post }
end
render 'show_with_edit'
end
def show_not_signed_in
#add methods here
@post = Post.find(params[:id])
respond_to do |format|
format.html # show_with_star.html.erb
format.json { render json: @post }
end
render 'show_with_star'
end
I am aware that for now the two different views are identical, I just put there different text for now. Once I have this nailed down I'll add to each view its own methods and content etc.
回答1:
You've put render in the wrong places.
def show_signed_in
#add methods here
@post = Post.find(params[:id])
respond_to do |format|
format.html # show_with_edit.html.erb
format.json { render json: @post }
end
render 'show_with_edit'
end
should be
def show_signed_in
#add methods here
@post = Post.find(params[:id])
respond_to do |format|
format.html { render 'show_with_edit' }
format.json { render json: @post }
end
end
Note the render that is moved to the format.html block.
The same applies for show_not_signed_in.
回答2:
please check your route.rb file is properly written , eg: consider following
resources :posts
collection
get :show_not_signed_in
get :show_signed_in
end
collection you write the method that is get or post
来源:https://stackoverflow.com/questions/15925620/error-template-is-missing-unable-to-have-2-show-views-for-the-same-posts