Not hitting controller method using nested resources - ruby-on-rails

I created an Open House for a specific listing. I am now trying to add an RSVP function. I keep getting an error and I can't figure it out. I'm sure it's something stupid, but I'm just not seeing it.
Rake Routes:
rsvp_listing_open_house GET /listings/:listing_id/open_houses/:id/rsvp(.:format) open_houses#rsvp
My Routes:
resources :listings do
member do
get 'like'
get 'unlike'
get 'duplicate'
get 'gallery'
delete 'gallery' => 'listings#clear_gallery'
get 'manage_photos'
get 'craigslist'
get "add_to_collection"
get 'request_photos'
end
resources :open_houses do
member do
get 'rsvp'
end
end
resources :listing_feedbacks do
member do
get 'archive_feedback'
end
end
end
My Controller:
def rsvp
#open_house_rsvp = params[:open_house_rsvp]
agent_id = params[:agent_id]
open_house_id = params[:open_house_id]
OpenHouseRsvp.create(:agent_id => agent_id, :open_house_id => open_house_id )
flash[:notice] = "Your RSVP has been submitted!"
redirect_to listing_open_houses_path
end
View:
<%= link_to "RSVP", rsvp_listing_open_house_path %>
Errors:
ActionController::UrlGenerationError in OpenHouses#index
No route matches {:action=>"rsvp", :controller=>"open_houses", :listing_id=>"5341"} missing required keys: [:id]

According to the error provided, the path helper is already receiving a :listing_id, and is expecting an :id from an open house object, so that it can properly construct the path. Assuming you've got an #open_house object defined in your controller, your path helper should look like this:
<%= link_to "RSVP", rsvp_listing_open_house_path #open_house %>
Hope this helps!

I do not have enough reputation to post comment so just summary the comments from Zoran and Nic above:
rsvp_listing_open_house_path requires 2 params: listing_id, open_house_id, you can use rsvp_listing_open_house_path(#listing, #open_house) or rsvp_listing_open_house_path(#listing.id, #open_house.id)
Make sure you have #listing and #open_house available in your controller action (you may name them differently). Otherwise, you will get the error mentioned in your comment: No route matches {:action=>"rsvp", :controller=>"open_houses", :id=>nil, :listing_id=>nil} missing required keys: [:id, :listing_id]

Related

Rails call a controller user-defined method

I am working with rails I have a controller name books and has a user defined method in it .I need to call this method so that i can see the output on console.And I dont want to call this method in helpers.
def approve
#user=current_user.users.find params[:id]
puts '#{#usery}'
end
Also I Have a link
<%= link_to 'approve',users_path,data: { :confirm => 'Are you sure to delete the folder and all of its contents?'} %>
.When i click on this link I want to call the above method on it .
You'll just need to define a route and call it through that:
#config/routes.rb
resources :users do
get :approve, on: :member
end
<%= link_to "Approve", users_approve_path(#user) %>
As #Rich suggested that, you can achieve it by member. Please note that when you'll create a member route in member block
resources :users do
member do
get 'approve'
end
end
then you'll get the params[:id]. Like
def approve
#user = User.find params[:id]
puts '#{#user}'
end
and when create a member route using :on then you'll get params[:user_id]. Like
def approve
#user = User.find params[:user_id]
puts '#{#user}'
end
Path will be same in both cases that is
<%= link_to "Approve", users_approve_path(#user) %>
Source Rails - Adding More RESTful Actions
Happy coding !!!

Rails "No route matches" error

I'm getting the following error in my Rails app:
No route matches {:action=>"edit", :controller=>"customers", :customer_id=>1}
Following is the jobs view line to link to edit customer details (customer has many jobs, but no nested resources):
<%= link_to job.customer_id, edit_customer_path(:customer_id => job.customer_id) %>
Following is edit definition in the controller:
def edit
if params[:customer_id]
#customer = Customer.find(params[:customer_id])
elsif params[:id]
#customer = Customer.find(params[:id])
end
respond_to do |format|
format.html # edit.html.erb
format.json { render json: #customer }
end
end
rake routes gives the following:
edit_customer GET /customers/:id/edit(.:format) customers#edit
NOTE:
If I change the view to:
<%= link_to job.customer_id, edit_customer_path(:id => job.customer_id) %>
then I get the same error but with ":id=nil" (i.e. with no value passed at all):
No route matches {:action=>"edit", :controller=>"customers", :id=>nil}
I am a little new to this, but can someone please explain what is going on?
Thanks!
Update
Try writing your path like this
edit_customer_path(job.customer) if job.customer
In your routes, specify one parameter for customers.
resources :customers, param: :customer_id
so you always know what the id is going to look like. (there is some trickery required to make this work all the way through resources).
Also, another potential issue (which has caught me out a few times) is that I have instantiated on the same page a blank instance of the same class I am trying to route to. eg #customer = Customer.new, which the link to is looking for, but doesn't find an id as the record hasn't been saved.
I find named routes a hassle, and much prefer using polymorphic urls as they degrade gracefully.
In your case it would mean something like this.
link_to 'whev', [:edit, #customer]

Routing Error uninitialized constant

I am trying to learn RoR.
MY controller is
class SectionController < ApplicationController
def new
if request.post?
u=SectionMst.new( :section_name => params[:section_name])
u.save
redirect_to("/section")
else
render
end
end
def index
#sections = SectionMst.all
end
def destroy
u=SectionMst.destroy(params[:id])
u.save
redirect_to("/section")
end
def edit
#user = SectionMst.find(params[:id])
end
end
and index.html.erb is
<%= link_to "Edit", edit_section_path(section.id), method: :edit %>
rake routes is
section_new POST /section/new(.:format) section#new
POST /section/:id/edit(.:format) section/:id#edit
section_index GET /section(.:format) section#index
POST /section(.:format) section#create
new_section GET /section/new(.:format) section#new
edit_section GET /section/:id/edit(.:format) section#edit
section GET /section/:id(.:format) section#show
PUT /section/:id(.:format) section#update
DELETE /section/:id(.:format) section#destroy
routes.rb is
post "section/new"
post "section/:id/edit"
resources :section
i am getting the
Routing Error
uninitialized constant Section
if i delete the second line of routes.rb
then i get
Routing Error
No route matches [POST] "/section/3/edit"
not able to get why???
Get rid of the first and second lines in your routes.rb. They're redundant. The resources will create these lines automatically.
The resources :section should be written as resources :sections. Notice that it's plural.
In your index.html.erb, you shouldn't mention method: at all. It's automatically set, and :edit as method doesn't exist. Method refers to put or get or delete, but you normally don't have to mention it.
You do not need this lines in your routes.rb
post "section/new"
post "section/:id/edit"
Change the third line to:
resources :sections #plural
If you delete them, you can hit the edit view using
<%= link_to "Edit", edit_section_path(section.id), method: :edit %>
which will hit your app at section/3/edit with a GET request.
In your edit.html.erb, you can then have fields to capture edits and do a PUT to /section/3.
Note that RAILS uses HTTP verbs to define the CRUD operations. Ref here.
Check your controller's file name because it should be plural. It is supposed to match the class name. So, you should rename app/controllers/section_controller.rb to app/controllers/sections_controller.rb.

Getting param in controller from GET request in Ruby on rails

I have in routes.rb
namespace :point do
resources :points do
get 'history'
end
In the view:
= link_to 'History', point_point_history_path(object)
Url looks like "/point/points/123456/history"
But in controller i cannot get it:
def history
raise params[:id].inspect
end
it returns nil.
What have i do wrong?
The route generated is
point_point_history GET /point/points/:point_id/history(.:format) point/points#history
So you want to request params[:point_id], not params[:id]

NoMethodError when going to page to create new record

I keep struggling with this sort of thing in Rails. I'm trying to create a statistics record for a given city. I guess I don't need to paste my models? Below is the error, URL, view and controller code.
Here's my error:
NoMethodError in Statistics#new
Showing new.html.haml where line #2 raised:
undefined method `statistics_path' for #<#<Class:0x007faf5e172928>:0x007faf5e717ef0>
The path I go to is:
http://127.0.0.1:3000/cities/1/statistics/new
Routes:
city_statistics GET /cities/:city_id/statistics(.:format) statistics#index
POST /cities/:city_id/statistics(.:format) statistics#create
new_city_statistic GET /cities/:city_id/statistics/new(.:format) statistics#new
edit_city_statistic GET /cities/:city_id/statistics/:id/edit(.:format) statistics#edit
city_statistic GET /cities/:city_id/statistics/:id(.:format) statistics#show
PUT /cities/:city_id/statistics/:id(.:format) statistics#update
DELETE /cities/:city_id/statistics/:id(.:format) statistics#destroy
cities GET /cities(.:format) cities#index
POST /cities(.:format) cities#create
new_city GET /cities/new(.:format) cities#new
edit_city GET /cities/:id/edit(.:format) cities#edit
city GET /cities/:id(.:format) cities#show
PUT /cities/:id(.:format) cities#update
DELETE /cities/:id(.:format) cities#destroy
routes.rb:
resources :cities do
resources :statistics
end
routes.rb:
Controller:
def new
#statistic = Statistic.new
respond_to do |format|
format.html # new.html.haml
format.json { render :json => #statistic }
end
end
View:
%legend New Stat
= form_for(#statistic) do |f| ###### ERROR HERE #######
= f.label :city_id
= f.text_field :city_id
.actions
= f.submit "Add", :id => "add-statistic", :class => "btn btn-primary"
EDIT added routes.rb
Two things.
As #nathan points plural form of city in English is cities so there's something wrong with your routes, maybe you renamed it? (please include routes.rb)
You have a route for statictic which is a nested resource of city, so for form helper you shoud pass
= form_for([#city, #statistic]) do |f|
Youre path file shows citys instead of cities. Perhaps you were bitten by an ActiveResource pluralize problem?

Resources