I have two lines in my routes.rb that look like this:
resources :locations
get '/locations/:session_series_token/new', to: 'locations#new', as: "new_location"
I want the create method in my LocationsController, and I also want a specific locations#new path.
Removing the resources :locations line causes my #new form to error with:
undefined method `locations_path' for #<<Class:0x007fa18caeb3b8>:0x007fa18caea8f0>
Extracted source (around line #1):
<%= form_for [#location] do |form| %>
<h2>Class Location</h2>
There must be a way to do this correctly!
I think that since you are removing resources: locations there is no longer an "index" route for Location (called locations_path). Run the command rake routes and you will see that you are only left with a new_location_path.
Try specifying the path like so:
<%= form_for #location, url: new_location_path do |form| %>
Try this:
resources :locations, except: [:new]
get '/locations/:session_series_token/new', to: 'locations#new', as: "new_location"
As the resources :locations defines all the routes for you and you need to define only the new route explicitly so you can write like above. If you remove the resources :locations then your create route also gets removed and the line <%= form_for [#location] do |form| %> searches for the locations_path which is the create action.
Hope this helps.
Related
Problem is that Rails can't find the new route after i changed how the routes for indis (stands for individuals or 'contacts') are generated.
in my routes file as a result of this statement
resources :profiles do
resources :indis
end
these routes were generated among others:
profile_indis
GET /profiles/:profile_id/indis(.:format) indis#index
POST /profiles/:profile_id/indis(.:format) indis#create
profile is the company and indis are individuals that work at the company
so i'm sitting here looking at this which is new.html.erb for new contacts.
<h1>New Contact</h1>
<%= render 'form', indi: #indi %>
<%= link_to 'Back', :back %>
I have the following line at the top of routes to avoid the rails router using the basic create route.
resources :indis, :except => [:create]
in routes
i get
No route matches [POST] "/indis"
hint: i added the :except code because the old route wasn't valid after i made the change
resources :profiles do
resources :indis
end
now when i hit create it can't find the route and i don't know what code
<%= render 'form', indi: #indi %> generates
somehow i need to fixup the new contact code so that when they hit create it routes to the new route instead of the old one (but I don't have access to the code of the form referenced in the new contact code.
I keep getting the following error. I'm trying to develop my own blogging platform as a way for me to learn and get better.
Error:
Showing /home/ubuntu/workspace/app/views/posts/show.html.erb where
line #36 raised:
undefined method `post_comments_path' for
<#:0x007f88f0044248> Did you mean? posts_path
app/views/posts/show.html.erb
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :author %><br />
<%= f.text_field :author %>
config/routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :posts
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root :to => "posts#index"
end
You do not have a comments resource nested in a posts resource (or at all) so you cannot create URLs using a comment within a post.
You need to define your comments routes before you can generate URLs for them.
If you want to nest them within posts, you should modify your current resources :posts line:
resources :posts do
resources :comments
end
resources in your routes files generate helper methods that Rails uses to produce URLs for models, in this case, post_comments_path.
You probably need to update it to look like
routes.rb
resources :posts do
resources :comments
end
after doing so you should be able see the new routes using rake routes or rails routes in the console.
You have to edit following changes to routes.rb file and try again
resources :posts do
resources :comments
end
Your route file is not correct. Place below code in your routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :posts do
resources :comments
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root :to => "posts#index"
end
This may not make sense but I'm trying to learn harder stuff and progress, it seems like I'm missing the ID for address but can't seem to find a solution.
I included the url in form_for because when I remove it, the app breaks. But seems like I predefined the url than edit breaks.
<%= form_for([#address.user, #address], :url => user_addresses_path) do |f| %>
Error Readout:
No route matches [PATCH] "/users/1/addresses"
When I remove :url=>
undefined method `user_client_address_path'
routes.rb
Rails.application.routes.draw do
# Security Devise Setup
devise_for :admins
devise_for :users
# Main Pages
root 'website/page#index'
# Client Sections
resources :users do
scope module: "client" do
root :to => 'dashboard#index'
resources :addresses
end
end
namespace :admin do
root :to => 'panel#index'
end
end
rake routes partial output (let me know if more is needed)
user_addresses GET /users/:user_id/addresses(.:format) client/addresses#index
POST /users/:user_id/addresses(.:format) client/addresses#create
new_user_address GET /users/:user_id/addresses/new(.:format) client/addresses#new
edit_user_address GET /users/:user_id/addresses/:id/edit(.:format) client/addresses#edit
user_address GET /users/:user_id/addresses/:id(.:format) client/addresses#show
PATCH /users/:user_id/addresses/:id(.:format) client/addresses#update
PUT /users/:user_id/addresses/:id(.:format) client/addresses#update
DELETE /users/:user_id/addresses/:id(.:format) client/addresses#destroy
If you are using AJAX to submit the form, try adding method: 'POST' to it.
If you are submitting the form normally, try adding method: :post to the form_for hash.
It should end up something like:
<%= form_for([#address.user, #address], url: user_addresses_path, method: :post) do |f| %>
I'm relatively new to rails and I've been struggling with this for a couple of days. I'd be much appreciated if you can see where I've gone wrong.
When I view the page in the web browser I get the following message:
Showing C:/Users/Matt/Documents/GitHub/Outputer/app/views/studies/index.html.erb where line #8 raised:
undefined method `studies_path' for #<#:0x6b03808>
8: <%= form_for #new_study do |f| %>
studies_controller:
def index
#line = current_user.lines.find_by_id(params[:line_id])
#machine = #line.machines.find_by_id(params[:machine_id])
#studies = #machine.studies.paginate(page: params[:page], :per_page => 10)
#new_study = #machine.studies.build
end
def create
#study = current_user.lines.machines.study.build(params[:study])
if #study.save
flash[:success] = "Study created"
else
flash[:error] = "Error : Invalid study description"
end
redirect_to :back
end
index.html
....
<section>
<%= form_for #new_study do |f| %>
<div class="field">
<%= f.text_field :description, placeholder: "New study description..." %>
</div>
<%= f.submit "Create", class: "btn" %>
<% end %>
</section>
....
Study Model
....
class Study < ActiveRecord::Base
belongs_to :machine
belongs_to :line
attr_accessible :avg_speed, :avg_uptime, :avg_yield, :description, :duration, :is_active, :start_time, :stop_time, :line_id
validates ....
has_many :events, dependent: :destroy
....
end
....
rake routes:
....
save_line_machine_study PUT /lines/:line_id/machines/:machine_id/studies/:id/save(.:format) studies#save {:has_many=>:machines}
line_machine_studies GET /lines/:line_id/machines/:machine_id/studies(.:format) studies#index {:has_many=>:machines}
POST /lines/:line_id/machines/:machine_id/studies(.:format) studies#create {:has_many=>:machines}
new_line_machine_study GET /lines/:line_id/machines/:machine_id/studies/new(.:format) studies#new {:has_many=>:machines}
edit_line_machine_study GET /lines/:line_id/machines/:machine_id/studies/:id/edit(.:format) studies#edit {:has_many=>:machines}
line_machine_study GET /lines/:line_id/machines/:machine_id/studies/:id(.:format) studies#show {:has_many=>:machines}
PUT /lines/:line_id/machines/:machine_id/studies/:id(.:format) studies#update {:has_many=>:machines}
DELETE /lines/:line_id/machines/:machine_id/studies/:id(.:format) studies#destroy {:has_many=>:machines}
....
routes.rb
resources :users
resources :lines, :has_many => :machines, only: [:index, :edit, :destroy, :show, :create] do
resources :machines, only: [:new, :create, :edit, :update] do
resources :studies
end
end
If I remove the form the page works fine which would suggest its in the form. I've tested the controller commands in the console and they all appear fine - I can create a new study object.
Thanks in anticipation
When you use form_for with a model instance, it defaults to the POST action for that controller which would be your studies_path. This is usually mapped to create in the controller.
From the looks of it, you need to add a route in routes.rb to handle that post request (see resources). You will also need a create method in your studies controller.
Here is a good guide for learning the basics of routing in rails.
Although a missing route is the most common reason for that (not-very-helpful) error, it can also be raised if one or both sides of a has_many/belongs_to relationship is missing or is incorrectly defined. Another place to look is a form field for an attribute that doesn't exist in the related model.
<%= form_for #new_study %> is equivalent to <%= form_for #new_study, url: studies_url %>. As your routes are defined differently, you need to pass the url you'd like to submit the form to to the url parameter (find form_for in the Rails API docs to see what other options it takes).
Three level deep nesting is kind of ugly to maintain, so I'd suggest the following:
resources :users
resources :lines do
resources :machines
end
resources :machines do
resources :studies
end
These shallow routes are much nicer to maintain. There's also a shallow: true option on nested resources calls, see the docs.
In your case:
# With the current setup
<%= form_for #new_study, url: line_machine_studies_path(#line, #machine)
# Same, my preference
<%= form_for [#line, #machine, #new_study] %>
# If you make your routes shallow,
# #line is not nescessary, as #machine holds all information about associations
<%= form_for #new_study, url: machine_studies_path(#machine) %>
# Same, my preference, what I would do
<%= form_for [#machine, #new_study] %>
General suggestions:
#study is preferred over #new_study. #study.new_record? will tell you whether the object is a new record if you need.
There's no has_many :... option on resources routes as far as I'm aware
Google rails shallow routes for more info. Keep nesting to two levels. Think about only what information you really require when creating objects and keep the URLs and url helpers as slim as possible.
I have the following three models:
Article
Article::Line (lines of the article)
Article::Review (reviews of a line)
I want to have a route that is
/articles/11/line/2/review/new
/articles/11/line/2/review/edit
My route.rb
resources :articles do
scope module: 'article' do
resources :lines do
resources :reviews
end
end
end
I am trying to make the form_for work with both new and edit automatically:
<%= form_for [ #line.article, #line, #review ] do |f| %>
However this will produce undefined method `article_article_line_article_reviews_path' error.
What have I done wrong, or is this possible?