Rails, get url to engine controller action by params - ruby-on-rails

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")

Related

ActionView::Template::Error - No route matches action in button_to \ link_to calling controller action

I'm trying to create a view with a button calling a controller action for a Redmine Plugin (unfortunately, I'm quite new at Rails). I'd need to create a method for my controller (here A Controller) so i can update some Active Records.
I created a controller:
redmine_a_controller.rb
class RedmineAController < ApplicationController
def prova(input)
puts input
return input
end
def p
puts 'vuoto'
returns 'vuoto'
end
end
The actions work correctly when called from the console. Unfortunately, when called from my view _a.html.erb:
<%=
button_to l(:imp_update), {
:controller => 'RedmineAController',
:action => 'p',
method: :post
}
%>
It returns this error:
ActionView::Template::Error (No route matches {:action=>"p", :controller=>"RedmineAController", :id=>"prova", :method=>:post, :tab=>"important_users"}):
I tried by calling the action on a new instance:
<%=
button_to l(:imp_update), {
:action => RedmineImportantUsersController.new.prova(1),
method: :post
}
%>
but it looks for the action in the Projects controller
ActionView::Template::Error (No route matches {:action=>"1", :controller=>"projects", :id=>"prova", :method=>:post}):
How can I make button_to call the correct prova method in the RedmineAController controller?
Thanks in advance for any help.
Add routes in routes.rb file
resources :redmine_as do
collections do
post :a
end
end
After that, Please fine the Routes url by typing on terminal below command
rake routes | grep p
Set the url in Link or button
link_to "Your Link Name", p_redmine_as_path
p_redmines_path it will be return by that "rake routes
| grep p" command.
As Ketan indicated, I was missing a route!
I put in my routes.rb:
resources :important_user do
collection do
post :u_update
end
end
and in my controller:
def u_update
puts params[:p_id]
puts "update!"
end

Link to another view using ruby in windows

I am doing a simple CR and trying to put some link/button(add,edit etc), when the user will click the add it will direct to that view..
Question: How should I indicate which view will be load when I click the specific link/button?
Index.html.erb
<h1>My First CRUD!</h1>
<%= link_to "Add Page", posts_path %>
<%= link_to "Edit Page", posts_path %>
<%= link_to "Showx Page", posts_path %>
Posts Controller
class PostsController < ApplicationController
def index
end
def addItem
end
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:item, :description)
end
end
Routes
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :posts
root "posts#index"
resources :posts
root "posts#addItem"
You can declare all the root routes that you want in your routes.rb, but it'll take just the one that was declared first, it doesn't matter if the definition has some mistake, it won't tell you.
And the route where the link_to will redirect is what you declare as second argument (in this case).
In your example you have three link_totags, which are pointing to the same path. If you'd like to make the "Edit" link_toredirect to the posts#edit method you firstly must create the edit method in your posts_controller and then add it to your route as:
get '/posts/edit/:id', to: 'posts#edit'
But in your case as you have resources :posts it makes the whole work for you and creates all the routes defined in the posts#controller, what resources do is to map a number of related requests to actions in a single controller, that's to say;
When Rails application receives a request for:
GET /posts/edit/1
That's what happen when you go to edit the a certain post by passing the id of 1, then it asks the router to map it to a controller action. If the first matching route is:
resources :posts
Then Rails dispatchs that request to the edit action on the posts controller with { id: '1' } in params.
Short answer, to create a common <a> tag create a link_to, add a text to specify to where will this redirect, then the route that you've defined, you can check them by running rails routes if you're using Rails 5 or bin/rake routes if you're with Rails < 5.

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.

Passing params using redirect to different action in controller

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

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]

Resources