dynamic link_to_remote in rails with jquery - ruby-on-rails

I'm trying to pass a string with a link_to_remote call as the :id, and the string should be collected from an input field with and id of "movie_title".
<div id="search_list">Nothing here yet</div>
<br />
<% semantic_form_for #movie do |f| %>
<% f.inputs do -%>
<%= f.input :title, :class => "movie_title" %> <%= link_to_remote( 'Search...', { :url => { :action => :imdb_search, :id => "'+$('\#movie_title').value+'" } }, { :title => "Search for this movie", :class => "imdb_search" } ) -%>
[...removed text that does not matter...]
<% end -%>
<%= f.buttons %>
<% end %>
I keep getting an javascript error, and if I remove the # from the jquery in the link, it returns "Undefined".
The link I get is:
<a class="imdb_search" href="#" onclick="jQuery.ajax({data:'authenticity_token=' + encodeURIComponent('yHPHYTZsPTQLi9JYSauUYcoie/pqPPk2uHBTN0PzNsQ='), dataType:'script', type:'post', url:'/movies/imdb_search/'+$('%23movie_title').value+''}); return false;" title="Search for this movie">Search...</a>
So I want the link updated with the contents of movie_title.
How do I do that?

I'd try something like
<%= link_to_remote( 'Search...', {
:url => { :action => :imdb_search},
:with => "'id=' + $('movie_title').value",
{:title => "Search for this movie", :class => "imdb_search"}
)

Fixed it
Used:
$('movie_title').val()
Insted of
$('movie_title').value

Related

Refactor ugly if/else statement in Rails View

I have this nasty if/else statement in a rails view:
<% if question.field_type == "text_area" %>
<%= f.text_area :content, :class=>"form-control question-field", :data => {:question => question.id, :filter=> #filter}, :value=> question.answer(#assessment).try(:content) %>
<% elsif question.field_type == "date" %>
<%= f.date_select :content, { :order => [:year, :month, :day], :prompt => { :day => 'day', :month => 'month', :year=> "year" }, :end_year=> Date.today.year, :start_year => Date.today.year - 2 }, {:class => "question-field", :data => {:question => question.id, :filter=> #filter}, :value=> question.answer(#assessment).try(:content)} %>
<% elsif question.field_type == "text_field" %>
<%= f.text_field :content, :class=>"form-control question-field", :value=> question.answer(#assessment).try(:content), :data => {:question => question.id, :filter=> #filter} %>
<% elsif question.field_type == "dropdown" %>
<%= f.select :content, options_for_select(question.options), { :prompt => "Choose One..." }, :class=>"form-control question-field", :value=> question.answer(#assessment).try(:content), :data => {:question => question.id, :filter=> #filter} %>
<% elsif question.field_type == "number" %>
<%= f.select :content, options_for_select(1..10), {:include_blank=> true}, :class=>"form-control question-field", :value=> question.answer(#assessment).try(:content), :data => {:question => question.id, :filter=> #filter} %>
<% elsif question.field_type == "percentage" %>
<h2>100%</h2>
<%= f.range_field :content, :value=> get_percentage(question), :class=> "question-field percentage", :data => {:question => question.id, :filter=> #filter}, :step => 25, :in => 0..100 %>
<% end %>
Is there a good way to refactor this to make it nicer? This piece of code is in every field:
:class=>"form-control question-field", :value=> question.answer(#assessment).try(:content), :data => {:question => question.id, :filter=> #filter}
Do I refactor into a helper method or a partial?
Sometimes templates are just messy and you can only clean up detail. Refactoring into a parameterized partial will help. For goodness sake, use a case. And consider switching to HAML. It eliminates a lot of the visual clutter.
<%= render 'question_field', f: f, type: question.field_type %>
Then in _question_field.erb,
<%= case type %>
<% when 'text_area' %>
<% f.text_area :content, class: 'form-control question-field', %>
<% data: { question: question.id, filter: #filter }, %>
<% value: question.answer(#assessment).try(:content) %>
<% when ... %>
<% end %>
Note common industrial practice is to pick a max line length and stick to it: 100 and 120 are pretty common. Also, use the new symbol key notation for hashes. The old hook-and-arrow is too noisy.
In HAML:
= case type
- when 'text_area'
- f.text_area :content, class: 'form-control question-field',
data: { question: question.id, filter: #filter },
value: question.answer(#assessment).try(:content)
- when ...
I would get rid of if's and when's altogether by creating seperate partial for every possibility, then you just end up with:
<%= render question.field_type, locals: {question: question} %>
Or to make it even cleaner for view make helper method and call only
<%= question_field(question) %>
and this method would look little bit like
def question_field(question)
return render question.field_type, locals: {question: question}
# raise when no partial found, or do something elese
end

nil_class when using partial

I have the following code:
new.html.erb
<% form_for #car,:html => {:name => 'car_form'} do |f| %>
<% time_created = Time.new %>
<%= f.hidden_field :CAR_DATE_CREATED, :value => time_created %>
<%= render :partial => "car_partial", :locals => {:object => #car } %>
<%= f.submit 'Create' %>
<% end %>
_car_partial.html.erb
<% fields_for #car do |f| %>
Origin: <%= f.select(:ORIGIN_TYPE,
[['Origin X', 'X'],
['Origin Y', 'Y'],
['Other origin', 'Other']
],{:prompt => "Please select"}
) %>
<%= observe_field("car_ORIGIN_TYPE", :frequency => 2,
:url => { :controller => 'car',
:action => :display_other_origin },
:with => "'id='+value") %>
<span id="otherOrigin" > </span>
<% end %>
controller code
def display_other_origin
origin = params[:id]
end
display_other_origin.rjs
if params[:id] == "Other"
page['otherOrigin'].replace_html :partial => 'car/other_origin_type', :locals => {:car => #car }
else
page.replace_html :otherOrigin, ''
end
_other_origin_type.html.erb
<% fields_for #car do |f| %>
If other, please state: <%= f.text_field :ORIGIN_TYPE, :size => 20%>
<% end %>
If user selects 'Other origin', the partial is displayed correctly but when i do <view source>,
Origin: <select id="car_ORIGIN_TYPE" name="car[ORIGIN_TYPE]">
<option value="X">Origin X</option>
<option value="Y">Origin Y</option>x
<option value="Other">Other origin</option>
</select>
<script type="text/javascript">
//<![CDATA[
new Form.Element.Observer('car_ORIGIN_TYPE', 2, function(element, value) {new Ajax.Request('/cars/display_other_origin', {asynchronous:true, evalScripts:true, parameters:'id='+value + '&authenticity_token=' + encodeURIComponent('+MtiIQj/KfX2huaetD1E7W7f2pu/HC2d31BhCPjcmlQ=')})})
//]]>
</script>
<span id="otherOrigin">
If other, please state: <input id="nil_class_ORIGIN_TYPE" name="nil_class[ORIGIN_TYPE]" size="20" type="text">
</span>
nil_class is displayed instead of 'car'.
Any help???
Thanks
Your controller action display_other_origin is not setting the #car variable - so it is nil when used in the display_other_origin.rjs file.
Basically, in your observe_field method you need to also set a parameter for the car id. For example:
<%= observe_field("car_ORIGIN_TYPE", :frequency => 2,
:url => { :controller => 'car',
:action => :display_other_origin },
:with => "'id='+value+'&car_id='#{#car.id}") %>
Then you can do #car = Car.find(param[:car_id]) in display_other_origin
...although I should note that convention states that params[:id] - when passed to the CarsController - should be the car id, and your origin should be params[:origin]

Rails: How to determine controller/action in view

This is my form partial:
<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>
<%= d.label :image, :label => 'Upload logo', :required => false %>
<%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px' %>
<%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
If the action is edit I want to show this instead:
<%= f.simple_fields_for :photo, :html => { :multipart => true } do |d| %>
<%= d.label :image, :label => 'Upload logo', :required => false %>
<%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px' %>
<%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
How can i achieve this?
current_page?(action: 'edit')
See ActionView::Helpers::UrlHelper#current_page?
Rails also makes the methods controller_path, controller_name, action_name available for use in the view.
Generally the form partial only contains the fields, not the form tag or the fields for, but if you have no other way, you can always see what params[:action] is currently set to and behave accordingly.
You could write something like
<%- form_url = #object.new_record? ? :photo_attributes : :photo %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
That is, if you have an #object to check against. Otherwise you could use action_name (and even controller_name).
So something like:
<%- form_url = action_name == :edit ? :photo : :photo_attributes %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
Hope this helps.
Rails 5: Display Action within the view
<%= action_name %>
If statement within the view
<% if action_name == "edit" %>
This is an edit action.
<% end %>
Just use #_controller.action_name in view

Problem with date_select on rails

I have following controller:
def personalization
#title = t "generic.forms.personalization"
end
def update_personalization
begin
#user.user_data.birthdate = Date.civil(params[:user_data][:"birthdate(1i)"].to_i,params[:user_data][:"birthdate(2i)"].to_i,params[:user_data][:"birthdate(3i)"].to_i)
rescue
wrong_data = 1
end
if #user.user_data.update_attributes(params[:user_data])
if wrong_data
flash[:Error] = t "generic.messages.error.wrong_data"
redirect_to :back and return
end
flash[:Success] = t "generic.messages.success.account_updated"
redirect_to :back
else
flash[:Error] = #user.user_data.errors.full_messages.join(".<br>")
redirect_to :back
end
end
and following view:
<div id="ProfileEditForm" class="ContentBorders">
<h1 class="FormsHeading"><%= t #title %></h1>
<div class="FormsSpacing">
<%= form_for(#user.user_data, :html => { :id => "UpdateUserForm", :class => "EditForms"}, :url => {:action => 'update_personalization'}) do |f| %>
<% flash.each do |key, value| %>
<div class="FormsMargins <%= key %>"><%=raw value + "." %></div>
<% end %>
<div class="Field"><div class="LabelInline"><%= t "generic.site.first_name" %>:</div>
<%= f.text_field :first_name, :id => "EditFirstName", :class => "Rounded5", :maxlength => "30" %></div>
<div class="Field"><div class="LabelInline"><%= t "generic.site.last_name" %>:</div>
<%= f.text_field :last_name, :id => "EditLastName", :class => "Rounded5", :maxlength => "30" %></div>
<div class="Field DateSelection"><div class="LabelInline"><%= t "generic.site.birthdate" %>:</div>
<%= date_select("user_data", "birthdate", :start_year => 1901, :end_year => 2011, :include_blank => true) %>
</div>
<div class="Field GenderSelection"><div class="LabelInline"><%= t "generic.site.gender" %>:</div>
<%= f.radio_button :gender, "0", :id => "EditGenderMale" %> <span><%= t "generic.site.male" %></span>
<%= f.radio_button :gender, "1", :id => "EditGenderFemale" %> <span><%= t "generic.site.female" %></span>
</div>
<div class="Field EducationSelection"><div class="LabelInline"><%= t "generic.site.educational_level" %>:</div>
<%= f.select :education_level, options_for_select({
" " => 0, (t "generic.site.education_levels.first") => 1, (t "generic.site.education_levels.second") => 2,
(t "generic.site.education_levels.third") => 3, (t "generic.site.education_levels.fourth") => 4,
(t "generic.site.education_levels.fifth") => 5, (t "generic.site.education_levels.sixth") => 6,
(t "generic.site.education_levels.seventh") => 7 }, #user.user_data.education_level) %>
</div>
<div class="Action"><%= f.submit (t "generic.forms.update_data"), :id => "EditSubmit", :class => "ProfileEditAction Shadow1 Rounded5 AcceptButtonsBorder" %></div>
<% end %>
</div>
</div>
<%= render :partial => 'profile_panel' %>
Now. The problem is with date_select method. Each of form field works properly (data from database fills them up), except that which was generated from data_select.
If I select some proper data, and click update button, then it saves that proper data to the db. Problem comes with the moment, when it is generated, and it doesn't come with any values (it's always empty when loaded).
Any ideas, how can that be fixed?
maybe...
you have:
<%= date_select("user_data", "birthdate", :start_year => 1901, :end_year => 2011, :include_blank => true) %>
you need:
<%= f.date_select("user_data", "birthdate", :start_year => 1901, :end_year => 2011, :include_blank => true) %>
just put the "f"
<%= f.date_select ...

Rails help simple form: undefined method `model_name' for Hash:Class

I am trying to create a form with the gem simple form.
The form should have a select list of all konkurrancers. And the action URL should be the controller public and action pricecompare and with the ID of the konkurrancer. My route: pricecombare/:id
My route.rb:
match "/finder/:id" => 'public#pricecompare'
My simple form so far:
<%= simple_form_for({:controller => "public", :action => "pricecompare"}, :method => "get") do |f| %>
<%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
<%= f.button :submit, :style => 'display: none;' %>
I get this error:
undefined method `model_name' for Hash:Class
The Html output I want:
<form method="post" id="new_konkurrancer" enctype="multipart/form-data" class="simple_form konkurrancer" action="/public/pricecompare" accept-charset="UTF-8">
<select style=" margin-left:10px;width:370px;float: left;
margin-top: 10px;"name="konkurrancer[form]" id="konkurrancer_form" class="select optional"><option value="">Vælg din A-kasse:</option>
<option value="1">ASE</option>
<option value="2">Træ-industri-byg</option>
<option value="3">Journalistik, kommunikation og sprog</option>
<option value="4">Faglis fælles A-kasse (3F)</option>
<option value="5">Danske lønmodtagere</option>
</select>
<input type="submit" value="Opret konkurrence" name="commit" id="konkurrancer_submit" class="button" style="display: none;">
Updated:
My public controller:
def index
#konkurrancer = Konkurrancer
end
def pricecompare
#akasse = Konkurrancer.where(params[:id]).first
#akasserne = Konkurrancer.order(sort_column + " " + sort_direction)
end
<%= simple_form_for(:konkurrancer, :url => {:controller => "finder"}, :method => "get") do |f| %>
<%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
<%= f.button :submit, :style => 'display: none;' %>
<% end %>
And I get this error in view:
ActionController::RoutingError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
No route matches {:controller=>"finder"}
Extracted source (around line #95):
92: <%= render("shared/forside") %>
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(:konkurrancer, :url => {:controller => "finder"}, :method => "get") do |f| %>
ActionController::RoutingError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
No route matches {:controller=>"finder"}
Update NEW error log:
Association cannot be used in forms not associated with an object
Extracted source (around line #96):
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(:konkurrancer, :url => '/finder', :method => "post") do |f| %>
96: <%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
97: <%= f.button :submit, :style => 'display: none;' %>
98: <% end %>
99:
I have tried to change it to a instanse variable and get this error:
NoMethodError in Public#index
Showing C:/Rails/akasse/app/views/layouts/application.html.erb where line #95 raised:
undefined method `to_key' for #<Class:0x85246c8>
Extracted source (around line #95):
92: <%= render("shared/forside") %>
93: </div>
94: <div style="width:900px;float:left;margin-left:29px;margin-top:15px;"><p style="width:500px; float:left;font-weight:bolder;font-size:12px;margin-left:20px;font-family:verdana;">Find den billigeste a-kasse - Vælg din a-kasse og se om du kan spare penge</p>
95: <%= simple_form_for(#konkurrancer, :url => '/finder', :method => "post") do |f| %>
96: <%= f.association :konkurrancer, :collection => Konkurrancer.all(:order => 'name'), :prompt => "Pick company" %>
97: <%= f.button :submit, :style => 'display: none;' %>
98: <% end %>
My Jquery:
$(document).ready(function() {
// send form ved klik paa listen
$('option').click(function ()
{
var form=$(this).closest('form');
$.ajax({
type:'post',
url:form.attr('action'),
data:form.serialize(),
success:function(msg){
$('#formcontent').html(msg);
}
});
});
});
Ok, since you want a jump navigation with a menu you can implement it something like this, just to give you an idea. It uses javascript and jquery by default but has a fallback if javascript isn't available.
In your view:
<%= form_tag "/finder/", :method => "get" %>
<%= label_tag :jump_nav, "Company" %>
<%= select_tag(:id, options_for_select(Konkurrancer.all(:order => 'name').collect { |k| [k.name, k.id] }), :id => "jump_nav") %>
<%= submit_tag "Jump to company", :id => "jump_nav_submit" %>
In your application.js or if you are using 3.1 assets, place in appropriate file.
$().ready(function() {
$('#jump_nav_submit').hide();
});
$('#jump_nav').live('change', function() {
window.location = "\/finder\/" + $(this).val();
});
Make sure jquery and application are included in your javascript_include_tags.
Then finally in your controller. Using a prepare statement will help prevent a SQL injection.
def pricecompare
#akasse = Konkurrancer.first(:conditions => ["id = ?", params[:id]])
end
I have no idea what Konkurrancer means so adjust labels accordingly.

Resources