I want to pass a parameter to the index action, but the I'm only getting the show action.
routes.rb:
Test1::Application.routes.draw do
resources :blog
end
blog_controller.rb:
def show
# code
end
def index
# code
end
View url that send to show action instead to index action:
My link
What should I add in routes file or in view?
Output of my routes:
$ rake routes
blog GET /blog(.:format) {:action=>"index", :controller=>"blog"}
blog GET /blog/:id(.:format) {:action=>"show", :controller=>"blog"}
The command line will show you routes you can use with rake routes
The route you want is blogs_path and you can add a parameter on to that, e.g. blogs_path(other_item => :value).
Exactly how will depend on whether you are try to use it in a controller, another view, etc.
For the view have: <%= link_to 'My Link', blogs_path(:other_item => value) %>
It sounds like you want 2 routes:
/blogs/:other_param
/blogs/:id
But, for as smart as Rails is, it can't figure out whether the param is meant to be treated as an other_param or as an id.
So the simplest solution is to add this route to the resources defaults like so:
resources :blogs
get "/blogs/other_param/:other_param", to: "blogs#index", as: "other_param_blogs"
That way Rails knows that if you're going to /blogs/other_param/current, then it will treat current as the :other_param.
Use below code to pass parameter:
My link
or
<%= link_to "My link", blog_path(name: "test") %>
above code will redirect to index action with name as key and test as parameter,
Related
My Task is to submit a form to place_order action inside Checkout controller.
This is how I wrote form in my view file i.e
<%= form_for (#order), url: {action: "place_order"} do |f| %>
It does reach inside this method and as I save object i want to redirect to some other method in the same class. This method name is thank_you. My code looks like this inside place_order method
if #order.save
redirect_to :action => 'thank_you'
else
...
end
But it redirects to show method of this class. If I change redirect to other class, it redirects fine but on other action of same controller, it always redirects to show.
Here is how I defined my routes
resources :checkout
resources :photos
devise_for :users
resources :carts
post 'checkout/place_order'
match 'checkout/thank_you', to: 'checkout#thank_you', via: [:get]
I need some expert opinion on this. Please help.
Move your thank_you route above resources :checkout.
From Rails guides:
Rails routes are matched in the order they are specified, so if you
have a resources :photos above a get 'photos/poll' the show action's
route for the resources line will be matched before the get line. To
fix this, move the get line above the resources line so that it is
matched first.
When I try to load the show view, displaying a "wiki" that was created, I get the error message Couldn't find Wiki with 'id'=
It specifically points to the second line in the Controller:
def show
#wiki = Wiki.find(params[:id]) ## shows the error in this line
end
def index
#wikis = Wiki.all
end
The following is my view:
<h1><%= #wiki.title %></h1>
<h3><%= #wiki.body %></h3>
And the following are my routes:
Rails.application.routes.draw do
get 'wikis/index'
get 'wikis/show'
get 'wikis/new'
get 'wikis/edit'
get 'wikis/create'
devise_for :users
get 'welcome/index'
root 'welcome#index'
end
Why am I getting this error message, and how can I get the view to load?
Thank you.
There is no :id param unless you pass it in explicity according to how your routes are set up. That is, you'd have to go to /wikis?id=<some id>, which is probably not what you want. Consider a more standard resourceful route setup:
Rails.application.routes.draw do
resources :wikis
devise_for :users
get 'welcome/index'
root 'welcome#index'
end
which lets you go to /wikis/<id> (that is, wiki_path(#wiki) in the controller) for the show route, and defines the index, create, etc routes for you correctly. http://guides.rubyonrails.org/routing.html
You have this error because you don't pass wiki's id into your show link, so the simplest solution is providing an id:
<%= link_to wiki.title, wikis_show_path(id: wiki) %>
But to be honest, your code looks very ugly with these custom routes to wikis controller which can be replaced by putting simply:
resources :wikis
in your routes.rb. This way, link to single wiki show page is also simpler:
<%= link_to wiki.title, wiki %>
You may want to read Rails routing guide.
Your params[:id] is most likely nil unless you pass it like /wikis/show?id=123, because route get 'wikis/show' does not have any params.
Most common approach is to have resources :wikis and routes like /wikis/123
add this line in your routes
resources :wikis
and remove this code:
get 'wikis/index'
get 'wikis/show'
get 'wikis/new'
get 'wikis/edit'
get 'wikis/create'
Now you can write in url: websitename.com/wikis to index, websitename.com/wikis/id, websitename.com/wikis/id/edit to edit... and get your id param...
Read more about resources in this link: Resources Link
I have a resource
resources :posts
So, I write
<%= link_to 'Изменить', edit_post_path(posts.id), method: :edit %>
And it opens the url:
http://178.62.102.154:3000/posts/3522/edit
And returns No route matches [POST] "/posts/3522/edit". But then I press on url and ENTER it opens. Why it doesn't open from the first time???
You should write it :
<%= link_to 'Изменить', edit_post_path(posts.id), method: :get %>
method: options takes the HTTP verbs as symbols.
Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code:
resources :posts
The above code created with all other actions, the #edit action as :
# GET edit_post_url(:id => 1)
def edit
# return an HTML form for editing a specific post
end
If you don't pass the value of the method: option, it is by default POST, as per the #link_to documentation.. So, your code hits the link using POST method, but the route file defined the action using GET method. That's why you got the error as you mentioned in the post.
I have a nested resource:
resources :res1 do
resources :res2
end
And I have a custom action in res2:
def my_action
end
which doesn't appear in the list of the pre-generated paths (there is no res1_res2_my_action_url url). I want to refer to my_action using controller and action notation but the following doesn't work:
url_for(controller: [:res1, :res2], action: :my_action)
Why is that?
The resources directive in your routes file will only create default routes for your controller.
#index
#new
#create
#show
#edit
#update
#destroy
If you want to add custom routes, you'll have to declare them like so:
resources :res1 do
resources :res2 do
get :my_action
end
end
you can hard code a specific route that points to action and controller:
get '/pathname', to: 'controller_name#my_action'
Try running rake routes and see what o/p you get,a try to apply in your view
get 'my_action' => "res2#my_action"
and then write
:url => my_action_path
I am putting a randomize def in my controller and would like to access it with a restful route. The route should be accessed with the following:
<%= link_to "Randomize", random_reader_path %>
But I cannot figure out how to get this route to appear in rake routes or configure it correctly in my routes.rb file.
The random method will do the same thing as index only provide a random page content #variable
Currently I have my reader Controller as
resources :reader
in my routes.rb
Add more RESTful actions!
resources :reader do
get 'random', on: :collection
end
The route will be random_readers_path, though.