I'm working with a model that I know is working (records exist in the data base, can be searched for and displayed in other views, etc.) but when I try to use the form_for tag to generate a view for editing one of these records, I get an error message:
Showing /var/www/caucus/app/views/registration_loader/checkIn.html.erb where line #13 raised:
undefined method `voter_path' for #<#<Class:0x98cabdc>:0x98c8878>
Extracted source (around line #13):
10: </div>
11:
12: <%= form_for(
13: #voter,
14: { :controller => "registration_loader",
15: :action => "editVoter"
16: } ) do |f| %>
The #voter refers to a Voter object retrieved by:
# Get the voter.
#voter = Voter.where( [ "voter_id = ?", #voterId ] )[ 0 ]
if not #voter
flash[ :error ] = "NO VOTER!"
redirect_to :action => 'search'
elsif not #voter.kind_of?( Voter )
flash[ :error ] = "NO VOTER RECORD! (#{#voter.class.to_s})"
redirect_to :action => 'search'
end
When I change the #voter to :voter, it stops giving me the error, but does not populate the fields in my view with the data for the record I want to edit.
According to the Rails 3.1 API guide, passing a model object into form_for should generate code that allows me to edit the data in that object, but evidently there is a missing helper method (voter_path). Where is this voter_path method supposed to be defined, and what is its proper semantic and signature? Nowhere in the documentation is creating such a method discussed, nor can I find any examples of writing such a method.
Is the *_path method supposed to be auto-generated? If not, can someone point me to the documentation that specifies the syntax and semantics of this method?
Thanks,
John S.
Short answer: don't use form_for unless you have also designed your code to use "resourceful controllers". Use form_tag instead. Adding resources :voters to routes creates routes to a non-existent controller.
Related
I have a haml ruby site that is pulling data from Salesforce. What I need to be able to do is to set an if condition based on the productCode listed in SF. The each.do establishes a loop to display the related data for each record in the loop.
The client would like productFamily specific pages. So I need to loop through all items with a product code of GEA for one page and GFE for another page.
.row
.col-xs-12
- #price_book.where(:productCode => GEA).each do |prod|
.row
.panel.panel-default
.panel-body
.col-xs-4
%img{:src => "#{prod.productUrl}", :height => "200", :width => "150"}
When I attempt to run this I get the following error:
ActionView::Template::Error (uninitialized constant ActionView::CompiledTemplates::GEA):
7:
8: .row
9: .col-xs-12
10: - #price_book.where(:productcode => GEA).each do |prod|
11:
12: .row
13: .panel.panel-default
Thank You for your assistance, I am new to Ruby and modifying another Developers code.
If #price_book is an ActiveRecord::Relation object, not just array of YourModel and productcode is String column of YourModel then you can call additional ActiveRecord methods like where:
#price_book.where(:productcode => 'GEA')
If GEA, GFE are not variables defined in view then values should be in quotes (double quotes) 'GEA', "GFE".
You can put
= #price_book.inspect
in your template and get more info about it. Tell us what kind of object is #price_book and we will give you advice.
I was able to apply the if filter logic to the loop with the following modification to the code
- #price_book.select{ |prod| prod[:productCode] == "GEA" }.each do |prod|
This returned only items from the price book with a product code of GEA
I need to show all my elements on the same page.
In routes:
namespace :nourishment do
resources :diets do
resources :nourishment_meals, :controller => 'meals'
get 'nourishment_meals/show_all_meals' => 'meals#show_all_meals', as: "show_all_meals"
end
end
which will generate:
nourishment_diet_nourishment_meals_path GET /nourishment/diets/:diet_id/nourishment_meals(.:format) nourishment/meals#index
POST /nourishment/diets/:diet_id/nourishment_meals(.:format) nourishment/meals#create
new_nourishment_diet_nourishment_meal_path GET /nourishment/diets/:diet_id/nourishment_meals/new(.:format) nourishment/meals#new
edit_nourishment_diet_nourishment_meal_path GET /nourishment/diets/:diet_id/nourishment_meals/:id/edit(.:format) nourishment/meals#edit
nourishment_diet_nourishment_meal_path GET /nourishment/diets/:diet_id/nourishment_meals/:id(.:format) nourishment/meals#show
PATCH /nourishment/diets/:diet_id/nourishment_meals/:id(.:format) nourishment/meals#update
PUT /nourishment/diets/:diet_id/nourishment_meals/:id(.:format) nourishment/meals#update
DELETE /nourishment/diets/:diet_id/nourishment_meals/:id(.:format) nourishment/meals#destroy
[**THIS**]
nourishment_diet_show_all_meals_path GET /nourishment/diets/:diet_id/nourishment_meals/show_all_meals(.:format) nourishment/meals#show_all_meals
The problem, when I do this:
<%= link_to "Show all meals", nourishment_diet_show_all_meals_path, :class=>"button green" %>
This error raise:
Problem:
Problem:
Document(s) not found for class NourishmentMeal with id(s) show_all_meals.
Summary:
When calling NourishmentMeal.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): show_all_meals ... (1 total) and the following ids were not found: show_all_meals.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.
The error is here, on my meals_controller.rb
private
# Use callbacks to share common setup or constraints between actions.
def set_nourishment_meal
#nourishment_diet = NourishmentDiet.find(params[:diet_id])
[***HERE***] #nourishment_meal = #nourishment_diet.meals.find(params[:id])
end
Method:
def show_all_meals
puts "This word does not appear"
end
Can someone help me?
The route below expects a :diet_id. A diet instance has to be provided as an argument for this path to call corresponding action.
nourishment_diet_show_all_meals_path GET /nourishment/diets/:diet_id/nourishment_meals/show_all_meals(.:format) nourishment/meals#show_all_meals
This should be changed:
<%= link_to "Show all meals", nourishment_diet_show_all_meals_path, :class=>"button green" %>
to:
<%= link_to "Show all meals", nourishment_diet_show_all_meals_path(diet), :class=>"button green" %>
Notice the argument (diet) above.
I think you should pass diet_id parameter in params. You should try something like this: <%= link_to "Show all meals", nourishment_diet_show_all_meals_path(#diet.id), :class=>"button green" %>. #diet.id is just an example, use whatever works for your application.
I am working on deleting a branch of a company using rails ajax.
Simple form for company account is -
= simple_form_for #company_account, :remote => true,
:url => company_account_path,
:method => :put do |f|
using this form i am creating, updating and deleting regions and branches of regions.
%div{ :id => 'branches_' + r.id.to_s}= render 'branches',f: f, r: region, company_account: #company_account
relation between company, region and branch is:
company has_many: regions
region belong_to: company
regions has_many: branches
branches belongs_to: regions
In this form i have a partial for displaying regions and branches, which uses form object of company account f. All this is working fine. I'm able to create new regions branches. Now i'm trying to delete branch using ajax.
When this call goes to controller i'm creating a form object for company account to render a partial like - In my controller
#f = view_context.simple_form_for #company_account, :remote => true,
:url => company_account_path,
:method => :put do |f|
render_to_string(:partial => 'company_accounts/branches', :locals => {f: f, r: #region, company_account: #company_account }).html_safe
end
and passing this #f object in responce using javascript as -
$('#branches_' + <%= #region.id%>).html('<%= escape_javascript #f %>');
$('#branches_' + <%= #region.id%>).show();
But unfortunately in response i am getting error -
undefined method `capture_haml' for #<#<Class:0xbe53d68>:0xcf9cb24>
Don't know what i am missing. Can any one please help??
Thanks in advance.
Update:
This is the Backtrace:
ActionView::Template::Error (undefined method `capture_haml' for #<#<Class:0xb9db2a4>:0xc953560>):
1: #inactive_branches
2: = f.simple_fields_for :regions, r do |reg|
3: %table.preferenceDetails
4: %tr
5: %td
app/views/company_accounts/_inactive_branches.html.haml:2:in `_app_views_company_accounts__inactive_branches_html_haml___356774371_104988750'
app/controllers/company_accounts_controller.rb:129:in `block in branches'
app/controllers/company_accounts_controller.rb:122:in `branches'
I fixed this issue by another way.
I am rendering a main partial of regions, in which form for #company account is created. Then kept both active/inactive branches partials in _regions.html.haml adding a condition on it.
When page is loaded it shows active branches by default and on the basis of request sent to show inactive branches then inactive branches partial is rendered.
I watched Railscast 328 this morning and I am having difficulty finding docs for a method.
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_boy_scout_path(boy_scout), :class => 'btn btn-mini' %>
I understand the link_to method, but I am confused about the t('edit .... ) parameter and it is in this method call twice. An explanation or even pointing me to some docs would be great. Thanks for all the help
The t function is an alias for I18n.translate.
The default: option gives the translation to use if the requested key is missing (the '.edit' of your example).
See guide in internationalization (and go to 4.1.2 for the syntax of the :default option)
Here's some background of my problem:
I am on Snow Leopard
I have RVM installed(using a ruby1.9.2-head installation)
I am using rails3
I installed the ym4r using this http://github.com/guilleiguaran/ym4r_gm (and followed the tutorial)
Anyway, I added these to my controller:
#map = GMap.new("map_div")
#map.control_init(:large_map => true,:map_type => true)
#map.center_zoom_init([75.5,-42.56],4)
#map.overlay_init(GMarker.new([75.6,-42.467],:title => "Hello", :info_window => "Info! Info!"))
then these to my view:
Test <%= raw(GMap.header) %> <%= raw(#map.to_html) %> <%= raw(#map.div(:width => 600, :height => 400)) %>
well actually im using haml(does it matter?)
Test
= raw(GMap.header)
- unless #map.blank?
= raw(#map.to_html)
#map{:style => "width: 600px; height: 400px"}
problem is i keep getting a
Showing /Users/eumir/rails_apps/evo-lux/app/views/layouts/_map.html.haml where line #11 raised:
can't convert Ym4r::GmPlugin::Variable to String (Ym4r::GmPlugin::Variable#to_str gives Ym4r::GmPlugin::Variable)
Extracted source (around line #11):
9: Test
10: = raw(GMap.header)
11: = raw(#map.to_html)
12: = raw(#map.div(:width => 600, :height => 400))
which is totally weird. I can't double check with debugger(it's another error altogether...my rails cant find ruby-debugger)
so im really kinda stumped. Any help?
The following function needs to be added to class Variable in mapping.rb of the plugin.
def to_str
#variable + ";"
end
Array#* gets called in the to_html function and Ruby 1.9.2 uses to_str instead of to_s to join the values.
Ok should've RTFM. Some discoveries:
the plugin was made for the now deprecated google maps v2 API
the to_html function of the plugin is a bit off in the sense that the init variables for the html variables is an array of variables - which are being joined by a string.