What's wrong with this Active Record line? - ruby-on-rails

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

Related

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) %>

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}"
%>

Pass parameter into controller from select box

Here is my view:
<%= form_tag({ :action => "display"}, :method => "get") do %>
<%= select(:music, :type, MusType::TYPES, {:include_blank => true}) %>
Here is my array constant in the model:
class MusType < ActiveRecord::Base
TYPES = ['Jazz','Rock','Blues']
end
My select menu draws values out of an array. How do I pass the selected value into the controller as a parameter after the submit button is pressed?
you can do something like below.
view code
<%= form_tag(:url => {:controller => "mycontroller" :action => "display"}, :method => "get") do %>
<%= select_tag(:music, :type, MusType::TYPES, {:include_blank => true}) %>
<%= submit_tag "Search", :id => 'search' %>
<% end %>
read the selected value in mycontroller.rb
def display
value = params[:music][:type]
// do something with value
end

Rails help with select and render partial

Want to create a select that tells what info to pass to the partial in a Rails 3 App.
Currently.
Lets say default is
<% orders_date_range = #orders.closed_today %>
Then
<div id="reports">
<%= render :partial => "report_details", :locals => { :orders => orders_date_range } %>
</div>
Can I create a select tag to pass in the orders_date_range? Or is there a better way of doing this?
UPDATE
<% orders = #orders.closed_today %>
<% #options = [["this week", #orders.closed_this_week],["this year", #orders.closed_this_year]]%>
<%= select 'orders', 'order', #options.collect {|f| [f[0], f[1]]}, :remote => true %>
<div id="reports">
<%= render :partial => "report_details", :locals => { :orders => orders } %>
</div>
Don't know why, but would only work with 'orders', 'order' for select.
applicaton.js
$("#orders_order").change(function(){
$.getScript("reports");
});
reports.js.erb
$("#reports").html("<%= escape_javascript(render :partial => 'report_details', :locals => { :orders => params[:selected] } ) %>");
Probably would be cool to do this as an AJAX thing....
Form.erb:
#call this at the top of your view
<%= javascript_include_tag :defaults %>
<%= select 'orders', 'option', #options.collect {|f| [f[0], f[1]]}, :onchange => remote_function(:url => {:action => :render_my_partial}, :with => 'Form.Element.Serialize(this)' %>
Controller:
def form
#options = [["option1", option1],["option2", option2]...]
end
def render_my_partial
render update do |page|
page.replace_html 'reports', :partial => 'report_details', :orders => params[:orders][:option]
end
end

rails how to call another controller in link_to_remote

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" %>

Resources