So, I''m pretty much a beginner, so I would appreciate some help with this issue. So, I have link_to helper (yes, I know that button_to is much easer, but for the needs of application it should be link_to), which should POST something in database without rederecting to any pages. <div class = ''source> <%= link_to(result_array[k], :controller =>''tests'', :action => ''add_key'', :method => :post, :keyword => result_array[k], :position=>k, :remote =>true) + result_array[k+1]%> </div> add_key method in tests_controller.rb: def add_key @test=Test.find(params[:id]) @key=@test.keys.build @key.keyword = params[:keyword] @key.position = params[:position] @key.save respond_to do |format| format.js end end But despite I added :method=>:post option in server log I still see server "GET" request. After an hour of hard googling I found out, that without form submitting link_to won''t create a "POST" request. Kinda weird, because I don''t need any forms here and the second trouble that Key exist only like a Model, I didn''t created controller or view for Key because generally I don''t need them. So I added form: <% form_for add_key_test_path do |f| %> <div class="actions"> <%= f.submit %> </div> <% end %> and add_key.js.erb to manage autosubmitting: $(''.source'').click(function(){ $(this).$(''.actions'').submit() }); But this doensn''t work. I spent a 2-3 hours trying to get this finally done, but can''t find mistake or wrong point. Any advice or help would be really appreciated. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/V5qoqaip0cYJ. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Mar-09 11:48 UTC
Re: Link_to if POST needed is a a hard nut to crack
On Saturday, March 9, 2013 7:05:45 AM UTC, Barry wrote:> > So, I''m pretty much a beginner, so I would appreciate some help with this > issue. > > So, I have link_to helper (yes, I know that button_to is much easer, but > for the needs of application it should be link_to), which should POST > something in database without rederecting to any pages. > > <div class = ''source> > <%= link_to(result_array[k], :controller =>''tests'', :action => ''add_key'', > :method => :post, :keyword => result_array[k], :position=>k, :remote > =>true) + result_array[k+1]%> > </div> > > > This is a classic: rails thinks in this case that :method and :remote areparameters for your controller action, rather than options to apply to the link generation process. You need something like link_to(result_array[k], {:controller => ''tests'', ...}, :method => :post, :remote => true) And you also need the rails javascript loaded so that it will add javascript handlers to the links that create and submit a form on the fly> t. > > Kinda weird, because I don''t need any forms here and the second trouble > that Key exist only like a Model, I didn''t created controller or view for > Key because generally I don''t need them. > > So I added form: > > <% form_for add_key_test_path do |f| %> > <div class="actions"> > <%= f.submit %> > </div> > <% end %> > > > and add_key.js.erb to manage autosubmitting: > > $(''.source'').click(function(){ > $(this).$(''.actions'').submit() > }); > >The thing you call submit on is the form, so something along the lines of $(''.source'').click(function() { $(''selector that will find the form'').submit() }) Fred> But this doensn''t work. I spent a 2-3 hours trying to get this finally > done, but can''t find mistake or wrong point. Any advice or help would be > really appreciated. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/KzjVrduduPAJ. For more options, visit https://groups.google.com/groups/opt_out.
Thaanks! finally works. After an hours I tried to set up this quite hard stuff, it just working looks like a magic +) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/oSFLYyM9U6oJ. For more options, visit https://groups.google.com/groups/opt_out.
andreo-FdlSlcb4kYpknbxzx/v8hQ@public.gmane.org
2013-Mar-10 22:08 UTC
Re: Link_to if POST needed is a a hard nut to crack
once again rails guides for the rescue http://guides.rubyonrails.org/form_helpers.html chapter 1.2 Multiple Hashes in Form Helper Calls "As with the link_to helper, the path argument doesn’t have to be given a string; it can be a hash of URL parameters recognizable by Rails’ routing mechanism, which will turn the hash into a validURL. However, since both arguments to form_tag are hashes, you can easily run into a problem if you would like to specify both. For instance, let’s say you write this:" "Here, method and class are appended to the query string of the generated URL because you even though you mean to write two hashes, you really only specified one. So you need to tell Ruby which is which by delimiting the first hash (or both) with curly brackets. This will generate the HTMLyou expect:" On Saturday, 9 March 2013 15:13:58 UTC+1, Barry wrote:> > Thaanks! finally works. > > After an hours I tried to set up this quite hard stuff, it just working > looks like a magic +) >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/yOlo2Pfz9ZIJ. For more options, visit https://groups.google.com/groups/opt_out.