New nested resource with link_to - ruby-on-rails

I have this route:
resources :projects do
resources :services
resources :contacts
resources :title_abstracts
resources :parcels
resources :leases
resources :documents
end
this is in my Projects View to add a new Contact to the Project:
new_project_contact_path(#project)
The url produced is: http://localhost:3000/projects/15/contacts/new
And the Contacts Controller:
def new
#project = Project.find(params[:project_id])
#contact = #project.contacts.build
end
def create
#project = Project.find(params[:project_id])
#contact = #project.contacts.build(params[:contact])
#contact.save
redirect_to project_path
end
But I get the following error:
Couldn't find Project with 'id'=
What am I doing wrong?
How should I test this?

In your routes.rb you nested :contacts in :projects.
but when you use simple_form_for, you cannot implicitly declare <%= simple_form_for #contact do |f| %>. You should pass the url:{controller: :contacts, action: :create, project_id:#project.id} as an argument along with the new #contact instance to simple_form_for. so that params[:project_id] will be available in your controller.
If you really wish to use <%= simple_form_for #contact do |f| %>, you need to add
resources :contacts to your routes.rb file but you need to optimize your controller action code for desired routing. I think it would help you.Thank you.

Related

Routing error for rails - uninitialized constant SubscribersController

I have a Subscriber model that takes in a "phone_number" and a "visit" integer. I have two controllers Subscribers and Visits(super and sub) I have never worked with nested controllers before and I'm having some issues with namespace I believe. Because I getting back the uninitialized constant error. Basically the subscriber controller signs up a subscriber and the visit controller counts the amount of times they've visited by user input of their phone_number. Why am I getting this error? I'll show my code for clarity.
CONTROLLERS
class Subscribers::VisitsController < ApplicationController
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if #subscriber
#subscriber.visit += 1
#subscriber.save
redirect_to subscribers_visits_new_path(:subscriber)
else
render "new"
end
end
end
class SubscribersController < ApplicationController
def index
#subscriber = Subscriber.all
end
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.create(subscriber_params)
if #subscriber.save
flash[:success] = "Subscriber Has Been successfully Created"
redirect_to new_subscriber_path(:subscriber)
else
render "new"
end
end
ROUTES
Rails.application.routes.draw do
devise_for :users
resources :subscribers, except: :show
get '/subscribers/visits/new', to: 'subscribers/visits#new'
root "welcomes#index"
VIEWS
<h1>hey</hey>
<%= form_for #subscriber do |form| %>
<div class="form-group">
<p>
<%= form.label :phone_number %>
<%= form.text_field :phone_number %>
</p>
<% end %>
ERROR
Hmm, my guess is you are trying to route url subscriber/visits/new to new action in VisitsController?How about changing this line:
get '/subscribers/visits/new', to: 'subscribers/visits#new'
to:
namespace :subscribers do
get '/visits/new', to: 'visits#new'
end
Also try to move this block above resources :subscribers, except: :show if you still get the error.
Cheers
You probably do not need to inherit one controller from another. Simply define the controllers as you normally would:
app/controllers/subscribers_controller.rb
class SubscribersController < ApplicationController
# methods for Subscribers
end
in app/controllers/visits_controller.rb
class VisitsController < ApplicationController
# methods for Visits
end
Note that these must to be located in separate files, so that Rails can find the correct source file by the name of the object that it's looking for. This is a Rails naming convention.
Regarding your routes, you'll need to change to use one of 4 route formats. Reading the section on Adding More RESTful Actions in the Rails Routing from the Outside In guide might help.
1) To route visits as a nested resource, which is what it appears you're actually trying to do, you would use this:
resources :subscribers, except: :show do
resources :visits
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
GET /subscribers/:id/visits
GET /subscribers/:id/visits/:id
GET /subscribers/:id/visits/:id/edit
PATCH /subscribers/:id/visits/:id/update
DELETE /subscribers/:id/visits/:id/destroy
This is the typical route structure for nested resources and separate controllers.
2) To make visits#new a simple collection (non-member) action in the VisitsController, then you likely want this:
resources :subscribers, except: :show do
collection do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/visits/new
POST /subscribers/visits
This is typically used to add new top-level routes in an existing resource and controller.
3) To construct visits as member actions, use this:
resources :subscribers, except: :show do
member do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
This is normally used to add new member routes in an existing resource and controller.
4) To simply make visits routes appear to be included in subscribers, you could use this:
get '/subscribers/visits/new', to: 'visits#new'
post '/subscribers/visits', to: 'visits#create'
resources :subscribers, except: :show
This will produce these routes:
GET /subscribers/visits/new
POST /subscribers/visits
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
This may be used to make arbitrary routes appear to be included in an existing resource, when they really may be independent.

Rails nested Resources not working in controller

I have this nested resource:
resources :services do
resources :users do
put "assign" => "services#users#assign", as: :assign
end
end
My form contains this:
<%= button_to 'submit', service_user_assign_url(service.id, abstractor.id), method: :put %>
this generates the following url, which looks fine to me:
http://localhost:3000/services/1/users/2/assign
and the following is in my services controller:
def assign
#service = Service.find(params[:service_id])
#service.users << User.find(params[:user_id])
redirect_to dashboards_path
end
However I get this error:
The action 'users' could not be found for ServicesController
I'm not sure what this means - I have a has and belongs to many relationship between users and services and I am trying to associate an existing user to a service
You need to tell rails if this action is a member action or a collection action, from the url you mentioned that you want to use, it's a member action:
resources :services do
resources :users do
member do
put :assign
end
end
end

Rails nested routing issue

I have Q+A model inside an events model and having issue with understanding how the nested routes work. I'm getting a No route matches {:action=>"create", :controller=>"event_questions" and missing required keys: [:event_id]`
My question form sits inside my show.hrml.erb for my event model right now. Some of the relationship stuff is from neo4j gem so it isn't standard but the issue shouldn't be related to that. From what I know, I am posting to /events/events_id/event_questions
events_controller.rb
def show
#event = Event.find(params[:id])
#event_question = EventQuestion.new
end
event.rb
has_many :out, :event_questions, type: 'questions_of'
event_question.rb
has_one :in, :events, origin: :event_questions
events/show.html.erb
<%= form_for [:event, #event_question] do |f| %>
#form stuff
<% end %>
event_questions_controller.rb
def create
#event_question = EventQuestion.new(event_question_params)
if #event_question.save
#event = Event.find(params[:event_id])
#event_question.update(admin: current_user.facebook_id)
#event_question.events << #event
redirect_to #event
else
redirect_to :back
end
end
routes.rb
resources :events do
resources :event_questions, only: [:create, :destroy]
end
I think it's in your routes. Change it to look like this:
resources :events do
resources :questions, controller: 'event_questions`, only: [:create, :destroy]
end
That'll give you http://youraddress/event/:event_id/questions and put it in the expected controller. Make sure it's event_questions.rb and not events_questions.rb. From terminal, also run rake routes and it'll show you the actions, paths, and controllers responsible for them.
Got it to work with
form_for(#event_question, :url => event_event_questions_path(#event)) do |f|
I'm not sure how to pass the #event via the other way (in the question), but assuming you can, both methods should work.

form_for for nested routes which namespaced models

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?

DRY routing for one polymorph resource with nested resources

Given the following models:
class Blog < ActiveRecord::Base
has_many :posts
end
class SiteBlog < Blog
end
class ProjectBlog < Blog
end
class Post <ActiveRecord::Base
belongs_to :blog
end
And the following routes:
resources :blogs do
resources :posts
end
In say a form partial, the following will work fine if #blog is a Blog:
form_for [#blog, #post] ...
However, if #blog is a ProjectBlog or SiteBlog, it bombs since it will be looking for a URL helper such as project_blog_posts.
I guess something like this would solve this:
[:project_blogs, :site_blogs].each |blogs| do
resources blogs do
resources :posts
end
end
I'm wondering whether there's a way to use the routes for subclassed models (e.g. ProjectBlog) to use the routes of the parent model (Blog). The "as" option only deals with the last object passed like [#blog, #post] to form_for.
Update
As requested below, here are the routes:
resources :blogs, only: [:show] do
resources :posts, only: [:new, :create, :edit, :update]
end
blog_posts POST /blogs/:blog_id/posts(.:format) posts#create
new_blog_post GET /blogs/:blog_id/posts/new(.:format) posts#new
edit_blog_post GET /blogs/:blog_id/posts/:id/edit(.:format) posts#edit
blog_post PUT /blogs/:blog_id/posts/:id(.:format) posts#update
blog GET /blogs/:id(.:format) blogs#show
Update 2:
The tip from an answer below:
form_for [#blog, #post], url: blog_posts_path(#blog, #post) do |f|
This works for "new" actions only, for "edit" actions, I'd get - as expected - a bad URL:
params[:action] # => "edit"
blog_posts_path(#blog, #post) # => "/blogs/publikationsreihe-tafelrunde/posts.5"
So the "if" I mentioned would fix this:
form_for [#blog, #post], url: params[:action]=='new' ? blog_posts_path(#blog, #post) : blog_post_path(#blog, #post) do |f|
But this looks incredibly clumsy, there must be a better way.
Easily solvable by passing the resource url to the form:
<%= form_for [#blog, #post], :url => blog_posts_path(#blog, #post) do |f| %>
...
<%- end %>

Resources