this is my form
<%= form_for([#game,#message_template]) do |f| %>
and in my controller I do this:
def edit
#message_template = MessageTemplate.find(params[:id])
#game = Game.where(:slug => params[:game_id])
end
My route is like this /games/:game_id/message_templates/:id
But I am getting this
undefined method `model_name' for #
Try setting that arel search to return the first object (for #game).
def edit
#message_template = MessageTemplate.find(params[:id])
#game = Game.where(:slug => params[:game_id]).first
end
routes.rb:
#based on your comment, this will give you all (and likely more) of the paths you will need.
resources :games do
resources :message_templates
end
Related
I'm following the Rails tutorial and making changes where appropriate, with the intention that my tutorial project will become a full-fledged production app after the completion of the tutorial.
I've run into a snag with the second model portion of the tutorial. Here is how I've written my second model.
In my policy.rb:
class Policy < ApplicationRecord
has_one :insured
end
In my insured.rb:
class Insured < ApplicationRecord
belongs_to :policy
end
In my routes.rb:
resources :policies do
resource :insured
end
In my insureds_controller.rb:
def create
#policy = Policy.find(params[:policy_id])
# next line is raising the error
#insured = #policy.insured.create(insured_params)
redirect_to #insured
end
private
def insured_params
params.permit(:name, :address, :phone, :email)
end
I've inspected the #policy object with render plain: #policy.inspect and can confirm that ActiveRecord is retrieving the policy correctly. When I inspect the attributes of #policy, using render plain: #policy.attribute_names.inspect, I don't see an insured attribute, which I thought Rails was supposed to automatically manage for me. In the tutorial, an article has_many :comments, and a comment is supposedly easily created and associated with the parent article with this call: #article.comments.create(comment_params). I also noticed that the tutorial uses params.require(:comment).permit(...) while I have to use params.permit(...), after inspecting the params hash I saw that the :insured attributes existed in the top-level of the hash, instead of being tied to an :insured key within the hash.
I tried manually saving and assigning the #insured object like so:
def create
#policy = Policy.find(params[:policy_id])
#insured = Insured.new(insured_params)
if #insured.save
#policy.insured = #insured
redirect_to #insured
end
end
Only to run into the following error in my .../insureds/new.html.erb:
<h1>New Insured</h1>
<h1><%= #policy.policy_number %></h2>
<%= render 'form' %>
<%= link_to 'Cancel', policy_path(#policy) %>
Which derives from my partial form .../insureds/_form.html.erb:
# the following line raises the error
<%= form_with model: #insured, local: true do |form| %>
# html omitted for brevity
<% end %>
Error: 'undefined method insureds_path'. This is weird because when I inspect the HTML I can see the form action for this view is /policies/[:id]/insured.
Sorry for the massive wall of text, I wanted to show you guys that I did try to figure out what is going wrong.
There is an error in your config/routes.rb file:
resources :policies do
# change it for:
collection do
get 'insured', to: 'policies#show_insured', as: 'show_policy_insured'
# maybe unnecessary to be here
# get 'insured/new', to: 'insureds#new', as: 'new_policy_insured'
# post 'insured/create', to: 'insureds#create', as: 'create_policy_insured'
# delete 'insured/delete', to: 'insureds#delete', as: 'delete_policy_insured'
end
end
# add resources here
resources :insureds
In policy_controller.rb:
def show_insured # 'policy/:id/insureds/
end
In insureds_controller.rb:
def show # '/insureds/:id'
end
def create
...
redirect_to show_policy_insured && return if #insured_policy
end
# before_filter or before_action
#policy = Policy.find(params[:id])
#insured_policy = #policy.insured
Check it and run this to see your routes:
$ bundle exec rake routes
get /policies/:id/insured => 'policies_controller#show_insured'
get /insureds/:id => 'insureds_controller#show'
get /insured/new => 'insureds_controller#new'
post /insureds/create => 'insureds_controller#create'
delete /insureds/:id/delete => 'insureds_controller#delete'
#maguri, that's not all necessary. The stumbling block I was running into was that Rails couldn't automatically determine the correct routes. When I provided my own urls in the form_with declarations, everything went smoothly.
Observe the following change in my _form.html.erb for the Insured model, which belongs_to Policy, which has_one Insured.
<%= form_with model: #insured, url: policy_insured_path(#policy) local: true do |form| %>
In my updated insureds_controller.rb file, using #Phlip's suggestion:
def create
#policy = Policy.find(params[:policy_id])
#insured = #policy.create_insured(insured_params)
if #policy.insured.save
redirect_to policy_insured_path(params[:policy_id])
else
render 'new'
end
end
This allows me to keep routes.rb clean and simple:
resources :policies do
resource: insured
end
Thank you for your answer, it helped me discover the problem was with my routes.
Hi everybody I'm from the old school using rails 2.
Actually I'm using rails 4 and I'm trying to find a way to create methods on the controller without writting
On RAILS 2 used: (only needed to write the name on the controller)
#controller
def report_a
end
def report_b
end
def report_c
end
...and whatever def
#ROUTES
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
On RAILS 4
#controller
def report_a
end
def report_b
end
def report_c
end
#ROUTES
match ':controller(/:action(/:id(.:format)))', :via => [:get, :post]
The problem is when I create a view report like this: (views/reports/report_a.html.erb)
<%= form_tag :action=>"report_a" do %>
<% end %>
I get this message:
No route matches [GET] "/reports/report_a"
To resolve this issue and doing Rails instruccions works like this:
#controller
def report_a
#users= User.search(params[:name])
end
def result_report_a
#users= User.search(params[:name])
end
#view/reports/report_a.html.erb
<%= form_tag :action=>"result_report_a" do %>
<% end %>
#routes.rb
get "reports#report_a"
post "reports#result_report_a"
get "reports#report_b"
post "reports#result_report_b"
get "reports#report_c"
post "reports#result_report_c"
Also I found this better way:
#controller reports.rb
def search_report_a
report_a
render :report_a
end
def report_a
#users = User.where(:name=>params[:name])
end
def search_report_b
report_b
render :report_b
end
def report_b
#users = User.where(:address=>params[:address])
end
...
#Routes.rb
resources :users do
match 'search_report_a', :via => [:post,:get], :on => :collection
match 'search_report_b', :via => [:post,:get], :on => :collection
...
end
Is there any other way to create methods without adding all inside ROUTES.RB ?
Any suggestions or the only way is adding get and post?
Imagine a case where you have several methods.
Best approach in Rails is to use REST architecture. Your controller should be able to view, create, update and destroy some resource (of course all actions are not mandatory).
For example:
def ReportsController
def index
# Actions to show links to all possible reports
end
def show
# Show report based on params
end
end
Your #show method may show any of report (report_a, report_b, etc) just by checking param from GET request.
And you don't need to make all logics inside #show method. It would be better to place report-related logic in, maybe, some service objects.
So I just Nested some resources that weren't Nested before and since I have been trying to fix all of the path references. The biggest issue I have been having is with the fact that there are 2 nested resources within a larger nested resource like so:
Users->Photos->Comments
On my form, it keeps giving me the following error
undefined method `model_name' for "/users/2/photos/2/comments/new":String
The error page says that the source is around line #1 of the following (my comments/_form partial):
= simple_form_for ([#comment, new_user_photo_comment_path(#user,#photos,#comment)]) do |f|
= f.input :content, label: "Reply to thread"
=f.button :submit, class: "button"
This is my Comments controller:
class CommentsController < ApplicationController
before_action :authenticate_user!
def new
#photo=Photo.find(params[:photo_id])
end
def create
#photo =Photo.find(params[:photo_id])
#comment=Comment.create(params[:comment].permit(:content, :id, :photo_id))
#comment.user_id= current_user.id
#comment.photo_id= #photo.id
#user= User.find_by(params[:id])
if #comment.save
redirect_to user_photo_path(#photo, #user)
else
render 'comments/new'
end
end
end
At first, it is not preferably to nest resources deeper than two times.
You should consider to nest comments within only photos. It`s ok to do like so in routes.rb:
resources :users do
resources :photos
end
resources :photos do
resources :comments
end
And you errors is because
= simple_form_for ([#comment, new_user_photo_comment_path(#user,#photos,#comment)]) do |f|
gives for method simple_form_for as parameters:
1 - model Comment
2 - String /users/2/photos/2/comments/new
to set proper path (form action) form builders need models as all arguments.
Maybe something like
= simple_form_for ([#user,#photos,#comment]) do |f|
should work
I have such problem: I don't need to nest all resources, because I need only two action.
First - show all reports related with 1 website.
Second - show all reports.
When I rewrite code in routes.rb like this:
resources :websites do
member do
match 'performance_reports' => 'performance_reports#index'
match 'performance_reports/show' => 'performance_reports#show'
end
end
//rake routes
performance_reports_website /websites/:id/performance_reports(.:format)
performance_reports#index
performance_reports_show_website /websites/:id/performance_reports/show(.:format)
performance_reports#show
Here is my action:
before_filter :load_website
private
def load_website
#website = Website.find(params[:website_id])
end
def index
#performance_reports = #website.performance_reports
end
Code from view:
<%= link_to "Link", performance_reports_website_path(website)) %>
but when I'm calling it I get error:
Couldn't find Website without an ID
rb:7:in `load_website'
What I'm doing wrong ?
In controller change the code to
#website = Website.find(params[:id])
I would like to use some routing like this:
resources :customers do
resources :electricity_counters, :shallow => true do
resources :electricity_bills, :shallow => true
end
end
Creating a electricity_counter works fine, but edit doesn't work as expected..
If I visit electricity_counters/1/edit I only get blank fields and all my data is missing.
My _form.html.erb for that starts like this
<%= form_for([#customer, #customer.electricity_counters.build]) do |f| %>
and the controller methods for new and edit are like this:
# GET customers/1/electricity_counters/new
def new
#customer = Customer.find(params[:customer_id])
#electricity_counter = #customer.electricity_counters.build
end
# GET /electricity_counters/1/edit
def edit
#electricity_counter = ElectricityCounter.find(params[:id])
#customer = #electricity_counter.customer
end
In debug it seems to be that my #customer variable isn't set right.. but maybe I'm just to stupid to use that aptana debugger;)
The electricity_counter model has the association to a customer set by:
belongs_to :customer
So what am I doing wrong?
Your problem is this line.
<%= form_for([#customer, #customer.electricity_counters.build]) do |f| %>
It builds a new electricity_counter no matter what you want to do. Since you're handling that in the controller.
But since you want to use the same _form partial for both new and edit you have to be able to change the form path. Basically I ended up doing something like this:
Controller
def new
#customer = Customer.find(params[:customer_id])
#electricity_counter = #customer.electricity_counters.build
#path = [#customer, #electricity_counter]
end
def edit
#electricity_counter = ElectricityCounter.find(params[:id])
#customer = #electricity_counter.customer
#path = #electricity_counter
end
Form
<%= form_for(#path) do |f| %>
Also your routes.rb is off change it to this
resources :customers, :shallow => true do
resources :electricity_counters, :shallow => true do
resources :electricity_bills
end
end