rails how to call another controller in link_to_remote - ruby-on-rails

how to call a different controller's action using link_to_remote
:url => {:controller => "Posts", :action => "update"} doesn't work

the method:
link_to_remote(name, options = {}, html_options = nil)
passing in a hash like:
link_to_remote "hug kittens", { :url => { :controller => 'kittens', :action => 'show' } }
as the second argument (options) works. verified.
the result:
<a onclick="new Ajax.Request('/kittens/hug', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('/BdZwHdC/QqtBJsdCU+cCHxabHj/QHUT6i8ggbr5CtY=')}); return false;" href="#">hug kittens</a>
The problem with your implementation might be, that there is no "real" update-url (except you created one by hand). Please have a look at the url of your edit-form. It's actually a post-request to "posts/:post_id".

<%= link_to_remote "Save", :url=>{:controller => "Posts", :action => "update"}, :update=>"div_id" %>

Related

What's wrong with this Active Record line?

This is throwing me a long error about missing a tIDENTIFIER. Can't seem to see anything wrong with it
<%= link_to_if(customer.try(:company_id).try(:blank?), "Company: #{#company.find(customer[:company_id].name}", { controller => 'companies', action => 'show', id => customer.company_id }) %>
<%= link_to_if(customer.try(:company_id).try(:blank?), "Company: #{#company.find(customer[:company_id]).name}", { controller => 'companies', action => 'show', id => customer.company_id }) %>
Missing bracket here #company.find(customer[:company_id].name

sending a parameter with Link_to and Data-no-turbolink

The following is very confusing to me:
The first link is correctly not using turbo links but the query is not being sent
The second link is the opposite scenario
= link_to 'yesturbo_noquery', "/controller/action", "data-no-turbolink" => true, query: "hello"
= link_to "noturbo_yesquery", {'data-no-turbolink' => true, :controller => "controller", :action => "action", :query => "hello" }
How do I make both work?
Edit, This works Thanks to Sikachu
= link_to 'yesturbo_yesquery', controller_action_path(:query => 'hello'), "data-no-turbolink" => true
link_to method actually consist of 3 parts:
link_to(name = nil, options = nil, html_options = nil, &block)
From both of the example you wrote there, example 1 mixed in query into the html_options, and example 2 mixed in data-no-turbolink into options.
I think if you changed it to this, it will work:
link_to 'noturbo_yesquery', {:controller => 'controller', :action => 'action', :query => 'query'}, :data-no-turbolink => true
I think following code is more correct:
<%= link_to('Product', #product, data: { no_turbolink: true }) %>
Also following code will works:
<%= link_to('Product', #product, 'data-no-turbolink' => true) %>

HAML Form For Rails

I'm currently trying to convert an ERB layout to HAML.
This is the error I keep receiving:
index.html.haml:18: syntax error, unexpected ')'
));}\n #{_hamlout.format_...
Here is the HAML page:
.row-fluid
.span6
%h2 Todo List
.span6
%h2{:style => "text-align:right;"} <script>document.write(today)</script>
%hr.divider
.row-fluid
.span6
%h2.small_head New Task
= render :partial => 'layouts/form_errors', :locals => {:object => #list}
.form
= form_for :list, :url => {:controller => 'lists', :action => 'create'} do |f|
= label_tag :list_name, "Title", :class => 'header_label'
I have also tried this as a variation:
= form_for(:list, :url => {:controller => 'lists', :action => 'create'}) do |f|
= label_tag(:list_name, "Title", :class => 'header_label')
Neither work and both generate the same error message, and help greatly appreciated.
You need to indent the code in the do block. This should work:
= form_for :list, :url => {:controller => 'lists', :action => 'create'} do |f|
= label_tag :list_name, "Title", :class => 'header_label'

passing ruby variable in observe_field method

I would like to know how can I pass a ruby variable inside an observe_field method.
I have the following:
<% action_parameter = params[:action] %>
<%= observe_field :car_type,
:url => { :controller => 'cars',
:action => :display_subtypes },
:with => "'id=' + value + '&somevalue=' + action_parameter"
%>
'action_parameter' is a variable and I would like to pass its value in the observe_field method but the code above does not seem to work.
Any suggestion?
try this
<% action_parameter = params[:action] %>
<%= observe_field :car_type,
:url => { :controller => 'cars',
:action => :display_subtypes },
:with => "'id=' + value + '&somevalue=#{action_parameter}'"
%>
Ruby variable will work in <% .... %>
you can use interpolation , Try this :
<%= observe_field :car_type,
:url => { :controller => 'cars',
:action => :display_subtypes },
:with => "id=#{value}&somevalue=#{action_parameter}"
%>

How to disable Rails submit buttons alongside Prototype helpers & RJS?

I'm trying to follow this post How can I unobtrusively disable submit buttons with Javascript and Prototype? but I can't get it to work. The form triggers an RJS function, so I need to keep the helpers' onclick events intact. The RJS returns/reloads the same forms along with two new texts. I'm really confused. Here is my rails code for the forms:
.span-20#comparison
/ new comparison . . .
/ voting forms (also reloaded)
.span-4.prepend-3.append-6
- form_remote_tag :action => url_for(:controller => :comparisons), :method => :post do
= hidden_field_tag :poem1_id, poems[:a].id
= hidden_field_tag :poem2_id, poems[:b].id
= hidden_field_tag :response, 1
= submit_tag "Vote for me", :disabled => false, :disable_with => 'Vote for me', :class => "compare"
.span-4.append-3.last
- form_remote_tag :action => url_for(:controller => :comparisons), :method => :post do
= hidden_field_tag :poem1_id, poems[:a].id
= hidden_field_tag :poem2_id, poems[:b].id
= hidden_field_tag :response, 2
= submit_tag "Vote for me", :disable_with => 'Vote for me', :class => "compare"
.span-4.prepend-8.append-8.prepend-top.last
- form_remote_tag :action => url_for(:controller => :comparisons), :method => :post do
= hidden_field_tag :poem1_id, poems[:a].id
= hidden_field_tag :poem2_id, poems[:b].id
= hidden_field_tag :response, 'draw'
= submit_tag "Declare Draw", :disable_with => 'Declare Draw', :class => "compare"
RJS
page.replace_html :comparison, :partial => 'poems', :object => #poems
page.insert_html :top, :previous, :partial => 'comparison', :object => #comparison
page << "Effect.ScrollTo($('top'));"
Here's one way you could do it using Prototype:
<script type="text/javascript">
document.observe("dom:loaded" function() {
$$("input .compare").each(function(submit) {
submit.observe("click", function() {
submit = true;
});
});
});
</script>
This adds an onclick event handler to every input element with a compare CSS class (i.e. your submit buttons) when the DOM is loaded. The onclick event handlers disables each button when it's clicked. Note that adding event handlers using Prototype this way does not replace any existing event handlers on the elements.

Resources