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]
Related
I'm writing a little Rails CMS and I'm a little stuck with a routing error. To begin with, I have a basic model called Entry, which other models are inheriting from. When I try to edit an existing model, it returns me an error
No route matches [PATCH] "/admin/posts/entries"
In my routes.rb in CMS plugin I have the following:
Multiflora::Engine.routes.draw do
root "dashboard#index"
scope "/:content_class" do
resources :entries
end
end
and in test app's routes.rb I have
mount Multiflora::Engine, at: '/admin'
In application_controller.rb I also tweaked routes a little:
def content_entries_path
entries_path(content_class: content_class.tableize)
end
helper_method :content_entries_path
def content_entry_path(entry)
entry_path(entry, content_class: content_class.tableize)
end
helper_method :content_entry_path
def new_content_entry_path
new_entry_path(content_class: content_class.tableize)
end
helper_method :new_content_entry_path
def edit_content_entry_path(entry)
edit_entry_path(entry, content_class: content_class.tableize)
end
helper_method :edit_content_entry_path
And in my show.html.erb I have this:
<%= link_to 'Edit', edit_content_entry_path(#entry) %>
When I navigate to edit_content_entry_path, it shows me edit page correctly, but when I try to save edited entry, it returns me an error stated above. When I run rake routes, it returns me the following:
entries GET /:content_class/entries(.:format) multiflora/entries#index
POST /:content_class/entries(.:format) multiflora/entries#create
new_entry GET /:content_class/entries/new(.:format) multiflora/entries#new
edit_entry GET /:content_class/entries/:id/edit(.:format) multiflora/entries#edit
entry GET /:content_class/entries/:id(.:format) multiflora/entries#show
PATCH /:content_class/entries/:id(.:format) multiflora/entries#update
PUT /:content_class/entries/:id(.:format) multiflora/entries#update
DELETE /:content_class/entries/:id(.:format) multiflora/entries#destroy
So, the error was in my edit.html.erb view -- instead of
<%= form_for(#entry, as: :entry, url: content_entry_path(#entry)) do |f| %>
I had
<%= form_for(#entry, as: :entry, url: entries_path do |f| %>
When I rewrote it, everything worked!
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]
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.
This is first action in controller:
def investor_following
#investor = params[:user][:investor_id]
# blah
end
def change_amount
investor = "xyz"
redirect to :action => :investor_following, :user[:investor_id] => investor
end
I am getting error how can I redirect to action investor following, what would be right syntax to do with params.
You should create a named route for your action in your routes.rb. I'm not sure what you investor_following function will do, so I am not certain if it should be a GET, POST, or PATCH. If you intend to modify your model, use a POST/PATCH, if not, use a get.
Once you have a named route, you will get a path helper like investor_following_path which you can send parameters as ruby objects:
#routes.rb
get '/investor_following', to: 'controllername#investor_following', as: 'investor_following'
#in your controller
redirect_to investor_following_path(user: {investor_id: investor})
This is untested but in general what you should do.
Here is info on redirect_to:
http://api.rubyonrails.org/classes/ActionController/Redirecting.html
Here is the info on routing for your named path:
http://guides.rubyonrails.org/routing.html
I have two isolated engines Offer and Prices.
How I can get url to Prices engine controller from Offers engine view, using hash with params?
#config/routes.rb
Rails.application.routes.draw do
mount Offers::Engine, at: "offers", as: "offers_routes"
mount Prices::Engine, at: "prices", as: "prices_routes"
end
#offers/offers_controller.rb
class Offers::OffersController
def show
end
end
#prices/prices_controller.rb
class Prices::PricesController
def index
end
end
#views/offers/show.html.slim
= link_to "Prices", { action:"index", controller:"prices/prices" }
In this case link_to raise error:
*** ActionController::RoutingError Exception: No route matches {:controller=>"prices/prices"}
I know about offers_routes.offers_path helper, but in my situation I should use hash with params.
You must pass the use_route param if you are using engine routes.
= link_to "Prices", { action:"index", controller:"prices/prices", use_route:"prices_routes" }
Source link: https://github.com/rails/rails/blob/v3.2.13/actionpack/lib/action_dispatch/routing/route_set.rb#L442
But this is more clear solution:
= link_to "Prices", prices_routes.url_for(action:"index", controller:"prices/prices")