How to send link_to parameters using POST on Rails 2 - ruby-on-rails

I have a link_to like this:
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, :class => "new_link" %>
There's a way to send those parameters not using the query string for it? (using post instead of a get) ??
Thank you!
I already tried:
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, {:method => :post,:class => "new_link"} %>
And it keeps doing the same thing...!

Add a supported HTTP verb in the method option.
<%= link_to "link", {:method => :post ...} %>

I think you can just add :method => :post to the options of link_to.
Here are the docs (for Rails 3, but I think this still holds true for Rails 2 also) http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
EDIT FOR UPDATED QUESTION
:method => :post belongs in the HTML options, not the URL options. This is not very obvious from the documentation.
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, {:method => :post, :class => "new_link"} %>

Related

How to work with ajax when showing different partials - Rails 3

So I was wondering how to work with the link_to method and ajax in Rails 3, when redering different partials.
Example:
Let say I have two link in show.html.erb and every link have a partial to render.
<li><%= link_to "Group1", user_path(#user), { :action => 'group1' , :method => :get, :remote => true} %></li>
<li><%= link_to "Group2", user_path(#user), { :action => 'group2' , :method => :get, :remote => true} %></li>
And we are going to render the partials in this div:
<div id="profile-data">
...render here...
</div>
In the UsersController we have our call methods for each partial:
def group1
respond_to do |format|
format.js
end
end
def group2
respond_to do |format|
format.js
end
end
And of course we have our js files in the view user folder:
group1.js.erb
$("#profile-data").html("<%= escape_javascript(render(:partial => 'group1')) %>");
group2.js.erb
$("#profile-data").html("<%= escape_javascript(render(:partial => 'group2')) %>");
So my question is:
is this the right way to render different partials with ajax? Are I missing something? Do have to route them some way?
This code dosent work right now and I dont know why, any help would be appreciated.
You need to explicitly state that you want to make a javascript request in your link_to.
This can be done be setting the format in the options hash to js with: :format => :js.
So in your case it should look like this:
<li><%= link_to "Group1", user_path(#user), { :action => 'group1' , :method => :get, :remote => true, :format => :js} %></li>
<li><%= link_to "Group2", user_path(#user), { :action => 'group2' , :method => :get, :remote => true, :format => :js} %></li>
The link_to should be somewhat like
<%= link_to "Group1", {group1_users_path, :format => :js} , :method => :get, :remote => true %>
or
<%= link_to "Group1", {:controller=>:users,:action=>:group1, :format => :js} , :method => :get, :remote => true %>
or if it is a member route and needs user_id
<%= link_to "Group1", {group1_users_path(#user) :format => :js} , :method => :get, :remote => true %>
The second param for link_to is the url options so only the url related option go in it, others should be out of the hash or else they will be just passed as params.
Check out more details at rails cocs they have some neat docs and examples
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
You need to have group1 and group2 in routes file
it should be like
resourses :users do
collection do
get "group1"
get "group2"
end
end
this will add helpers group1_user_path and group2_user_path
I recemmond you to go through rails docs thoroughly
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Routes setting when press the button generated by link_to tag

When I click the link below, it goes to /messages/discard.1 instead of /messages/discard/1 .
Any idea what am I doing wrong?
My View:
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
messages_discard_path(#messages.id),
:method => 'post',
:confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
:class => 'btn btn-danger' %>
My Routes:
match '/messages/discard' => 'messages#discard', :via => :post
Try revising the link_to method and use:
messages_discard_path(#messages)
Instead.
Also, it looks like that route is missing :id in it. I'm assuming you need match '/messages/:id/discard' => 'messages#discard', :via => :post instead.

Rails Ajax Render Partial yields 500 Internal Server Error and Java::JavaLang::NullPointerException

This symptom has been reported in various contexts, most of which I've already searched yet found no clear answer. Here is my context.
In full view index.html.erb:
<div id="add_thingy" style="display:none;">
<%= form_tag (
:action => 'create',
:remote => true,
:update => "thingy_list",
:position => :bottom,
:html => {:id => 'thingy_form'}
) do %>
ipv4_address: <%= text_field "thingy", "ipv4_address" %>
<%= submit_tag 'Add' %>
<% end %>
</div>
In partial view _index.html.erb:
<li><b>
<%= link_to thingy.ipv4_address, "http://"+thingy.ipv4_address+"/index.html", :target => '_blank' %>
<%= link_to 'page1', "http://"+thingy.ipv4_address+"/page1.html", :target => '_blank' %>
<%= link_to 'page2', "http://"+thingy.ipv4_address+"/index.html", :target => '_blank' %>
<%= link_to 'Edit', {:action => "edit", :id => thingy.id} %>
<%= link_to 'Delete', {:action => "delete", :id => thingy.id},
:confirm => "Delete thingy at #thingy.ipv4_address?", :method => :delete %>
</b></li>
In thingy_controller.rb (other things I've tried are shown commented out):
class ThingyController < ApplicationController
layout 'standard'
def index
...
end
...
def create
#thingy = Thingy.new(params[:thingy])
if #thingy.save
# redirect_to :action => 'index'
# $(“#index”).rhtml(“<%= escape_javascript render( #thingy ) %>”);
# render :partial => 'index', :object => #thingy_list
# render :partial => 'index'
render :partial => 'index', :object => #thingy
end
end
...
end
All other aspects of my implementation test clean, including the first commented line above. It's when I try any of the subsequent 4 lines that I get the indicated errors.
I'll use the first commented line for now, though it defeats the main purpose of using Ajax.
Speculation abounds on this question, so I'm looking for information on how it has been fixed in a similar context rather than suggestions on how it might be fixed.
On behalf of myself and the community, thanks for your help!

Additional Parameter in form_for in Rails

Is it possible to submit another parameter outside the form data in rails? My problem is that I render different forms for different classes and send them to the same create method. I would like to send the class with the form (as value not as key in the hash).
Something like the :type parameter (that actually doesn't work)
<%= form_for(#an_object, :url => { :controller => :a_controller, :action => :create },
:type => #an_object.class.to_s.underscore) do |f| %>
The post message looks like:
{"commit"=>"Create Class of an Object",
"authenticity_token"=>"/iqu0A8/AocDT3HyjL5/+bKZiLkyr4FE71u/mc8Wx0Y=",
"utf8"=>"✓",
"class_of_an_object"=>{"name"=>"a name",
"description"=>"a description"}}
and I would have a "type" => "class_of_an_object", but directly in the hash an not within the "class_of_an_object" hash.
<%= form_for #an_object,
:url => { :controller => :a_controller,
:action => :create,
:type => #an_object.class.to_s.underscore } do |f| %>
And I prefer to use named routes
<%= form_for #object, :url => object_path(#object, :type => "whtever"), :html => {:method => :post} do |f| %>
This works for me:
<%= form_for #foo, :url => foo_path(:type => "whatever"), :html => {:method => :post} do |f| %>

Generating link path with parameters

I've got model with many associations.
User :has_many => :posts, :videos, :images etc
I've got helper method, which receives one of this association
draw_chart user.videos
def draw_chart(assoc)
assoc.each do |a|
link_to "update", [a.user, a], :method => :put, :remote => true, :confirm => 'orrly?'
end
end
Problem here is to send extra parameters to update action.
For example I want to get this url: /users/1/videos/2?status=done but I am confused how to realize this while I am using [a.user, a] instead of user_videos_path(video, :status => 'done'). But I can't use last, because it can be either videos, or images or anything else
I wont go using eval, how about this
<%= link_to('update', self.send("user_#{a.class.name.pluralize}_path", a, , :status => 'done'), :method => :put, :remote => true, :confirm => 'orrly?') %>
Only solution I can think up is this strange one:
link_to "update", eval("user_#{a.class.name.tableize}_path(#{a.school.id}, #{a.id}, :status => 'done')"), :method => :put, :remote => true, :confirm => 'orrly?'
but this looks messy

Resources