nil_class when using partial - ruby-on-rails

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]

Related

How can I use drop down box with Ajax to render a partial in Rails?

I created drop down menu layouts as follows.
<select name="search" id="search" >
<option value="0">Please select</option>
<option value="1">Name</option>
<option value="2">Trainer</option>
<option value="3">Venue</option>
<option value="4">Date</option>
</select>
<div id="div_to_be_updated" style="float:right"></div>
<%= observe_field 'search',
:update => 'div_to_be_updated',
:url => {:controller => 'events', :action=> 'find' },
:with => "'is_delivery_address=' + escape(value)" %>
Then I created find method in my controller.
def find
if params[:is_delivery_address] == "1" || "2" || "3"
render :partial => 'layouts/new_search'
else
render :partial => 'layouts/nothing'
end
end
Then I created partial called _new_search.
<span style="text-align: right">
<% form_tag "/events/find_search" do %>
<%= text_field_tag :search_string%>
<%= submit_tag "search" %>
<%end%>
</span>
Again I created find_search method in controller.
def find_search
events=Event.find(:all, :conditions=>["venue = ?", params[:search_string]])
end
Then I created view page. But when I searched somehting it will give exception.
NoMethodError in Events#find_search
Showing app/views/events/find_search.html.erb where line #10 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
What should I do? Can anybody help me>
Not sure if this is it - but the following line has a bug
if params[:is_delivery_address] == "1" || "2" || "3"
this will always be true, since if params[:is_delivery_address] != "1" then the result will be "2" (non-nil - ie true).
Try irb to check:
> params = {:is_delivery_address => "0" }
> params[:is_delivery_address] == "1" || "2" || "3"
=> "2"
I have this working in Production:
<% form_for :youractionhere, :url => { :action => "youractionhere", :param1 => #market, :param2 => #param2 } do |f| %>
<%= select_tag :search, options_from_collection_for_select(#search_options, "search_option_name", :search_option_name, :search_option), { :onchange => "this.form.submit();" } %>
<% end %>
The key is to have the :url => { :action => "youractionhere"} and also the {:onchange => "this.form.submit();"} in it. The action, of course, should render the partial when called.

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.

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

dynamic link_to_remote in rails with jquery

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

Resources