Template is missing when using redirect_to?
In my Rails 3 app, I have a Results controller. When im on the show.html.erb page using the show action in the controller, I have a link to a filter action. When the filter action is done, i want it to redirect back to the show.html.erb, but for some reason it keeps saying Template is missing Missing template results/filter
. Im using ajax, so there is a fitler.js.rjs file, but i cant figure this out. If i remove the filter.js.rjs file, it gives me Unknown action The action 'filter' could not be found for ResultsController
Heres my code for the results action. Any help is appreciated.
def filter
@dispatches = Dispatch.find_by_message_ids开发者_StackOverflow(params[:message_ids]) unless params[:message_ids].blank?
unless @dispatches.blank? || @input_messages.blank?
@output_messages = OutputMessage.find_by_dispatch_ids(
@dispatches.collect{|d| d.id }.uniq
)
end
respond_to do |format|
format.html { redirect_to show_path }
format.js
end
end
UPDATE
Here's the code on show.html.erb that calls the filter action
<%= form_tag(:controller => "results", :action => "filter", :remote => true) do %>
redirect_to show_path won't work; the "show" action expects an id or an object instance. In Rails 3 you can redirect directly to an object, e.g. redirect_to @user which will default to the show method on that object. You can also redirect to the action with redirect_to(:action => "show", :id => NN) where you supply the id manually and optionally specify the controller. Or in the old-fashioned way redirect_to show_user_path(@user).
Your form has :remote => true
and this prompts the rails that the incoming request is an ajax call and hence rails would look for filter.js.rjs file and it would never look for the html file. hence u get the error when u remove the rjs file.
Got it to work. The problem was my form tag. It was outputting action="/results/filter/?remote=true"
so i changed my form_tag to :
<%= form_tag url_for(:controller => "results", :action => "filter"), :remote => true do %>
And it worked! But that uses HTML5, now im going to find a workaround for non-HTML5
精彩评论