开发者

Generating a form that passes multiple parameters

开发者 https://www.devze.com 2023-01-04 18:12 出处:网络
I have the following in my routes.rb map.diff \'posts/:id/diff/:from/:to\', :controller => \"posts\",

I have the following in my routes.rb

map.diff 'posts/:id/diff/:from/:to', :controller => "posts",
                   :action => "diff", :conditions => { :method => :get }

And I have the following in my view file.

- form_tag(diff_path(), :method => :get) do
  = text_field_tag(:from, "")
  = text_field_tag(:to, "")
  = hidden_field_tag(:id, @post.id)
  = submit_tag("Submit")

I would like to generate a form that submits something like "http://example.com/posts/3/diff/13/18", but it fails. How can I make such a form?

I need to pass parameters for diff_path(), but I don't know how to do that. I don't even know if this is possible with form_tag.

The error message:

diff_url failed to generate from {:action=>"diff", :controller=>"posts"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. 开发者_Go百科 content_url has the following required parameters: ["posts", :id, "diff", :from, :to] - are they all satisfied?


To my knowledge, what you're trying to accomplish can't be done with just an HTML form. The reason being that the form will only know how to submit traditionally via GET and POST. It has no knowledge of the structure of the URL.

You get that error message because the :id, :from and :to parameters are required to form the both you want, so when you call diff_path() it freaks out.

Personally, I would advise you not to use the URL structure you're planning on - however I'm not totally clear on what this page is going to display. Regardless, if the :from and :to parameters are algorithmic input and not resource identifiers, I would avoid this structure.

That said, if you do want to implement this, you would either have to implement a redirect from rails or javascript.


Rails method

#some_controller.rb
redirect_to diff_path(:from => params[:from], :to => params[:to])

Javascript (jQuery) method

$(function() {
  $("form#your_form_id_here").submit(function() {
    window.location = "posts/" + this.id + "/diff/" + this.from + "/" + this.to;
    return false;
  });
});


I don't think that will work by specifying the url with that format in the form. Rails tries to create the URL to submit to when you render the page, not when you submit the form. So it is not seeing those parameters when you render the page. I would recommend creating another route

map.diff_form 'posts/:id/diff', :controller => :post, :action => :diff, :conditions => {:method => :post}

You could use the two routes side by side if you need to keep the current url format.


Why are you trying to do this in the first place? I really can't think of a good reason why this would be necessary.

Your "from" and "to" variables should probably just be normal URL parameters - i.e. /posts/:id/diff?from=X&to=Y, so that you can then retrieve them in your controller with params[:from] and params[:to]

There may be a way to make Rails work this way, but you're going to have issues with it, since Rails is emphatically not meant to work this way.


I think you can use like this

diff_path(@id,@from, @to)

where @id, @from, @to are instance variables. If dont, you can specify a hash also like

diff_path(:id=>@id,:from=>@from, :to=>@to)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号