I am using Friendly_ID for slugs on my site and it is working...sort of. I can access the routes correctly, but when I follow a link to the page, it still shows the id. For example, I have a link that shows the following:
<%= link_to "X", location_path(#location, :id => "1")
which displayed the url as localhost:3000/locations/1 so I changed the link to:
<%= link_to "X", location_path(#location, :id => "X")
which displays properly (localhost:3000/locations/X) but when you look at the href of the link in after you navigate to its page, it is displayed as <a ... href="/locations/X.X">...</a>.
Why is the link displaying X.X and not simple X?
You don't need to pass id to the location_path. It will be picked up from #location. Drop id from location_path and it should fix the problem.
Related
In my rails project there is a controller and view named "Welcome/index" where index is an action and another one named "home/page" . As i set root"home#page" as my root page. Now i want to transfer from "page.html.erb" into "index.html.erb" . How can i do that. And the code i written is below.Do i have to enter some thing in my controller class. please suggest.
these are the links that i tried. (How to create an anchor and redirect to this specific anchor in Ruby on Rails)
<a rel="nofollow" href="index.html.erb">Transfer to index</a>
You are not supposed to link to .html.erb files, you should link to the methods (not exactly the name of the method, but the name of the route) of a controller.
I strongly encourage you to review the ruby on rails MVC principles. You can read about routing and linking aswell.
Responding to your question, check out the command "rake routes". It will list the defined routes of your app and helps you to use them.
Try to replace your code by this:
<%= link_to 'welcome', welcome_path %>
<%= link_to "Link", controller:"controllername" %>
is the code you should use
You need to make sure a named route is defined for welcome/index and then can use the Rails helper link_to to automatically build your link for you in the view.
In routes.rb:
match '/welcome' => 'welcome#index', :as => :welcome
In your page.html.erb view:
<%= link_to 'Go to Welcome Page', welcome_path %>
If you want go for index action of Welcome controller then you can use:
<%= link_to "Transfer to index", welcome_path %>
Check the rake routes for path.
Plese refer link
I am a totally new to learn rails and just start this Ruby on Rails Guide website.
And, i am a little confused when I following some step.
1.Why "redirect_to #post" will get "show action"?
2.Why we need use "index action" to list all posts?
3.When we use "<%= link_to "My Blog", controller: "posts" %>", I know it will link to posts controller, but don't know why will display index action which list all posts?
Thanks.
"show" action is to display a object detailed information, and the object is #post.
"index" action is to display one sort of resources.
You can use the default action, but you can customize it at well, like below:
.<%= link_to "Blog", {:action => "any_action", :controller
"posts_or_any_controller"}, {:method => :get} %>
I have a link_to code
<%= link_to "#{(pages_counter/2) + 1}", { controller: "videos", action: 'videos_navigate', offset: pages_counter }, remote: true %>
When clicking on the link it's passing the id of the video automatically meaning I didn't explicitly pass a video object or an id via the link_to code. Is this happening because I directly identified the controller and the action as seen in the code above? Thanks in advance
sample URL generated: /videos/videos_navigate/1?offset=2
If you are on a show page, where the ID is in the URL already, and your link_to doesn't specify an ID, it will pick up the ID from the URL . The same thing would happen if you did not specify the controller, it would instead grab the current controller you are in.
So if you went to record 2 and click the same link, your URL will be /videos/videos_navigate/2?offset=2
Why don't you link directly to the route instead. As in, run rake routes int he console, it should print out an path name for your videos_navigate path... then you can presumably link to it like this:
<%= link_to "#{(pages_counter/2) + 1}", videos_navigate_path, :remote => true %>
If it doesn't already have a path, then you can give it one by adding :as => 'videos_navigate' to your route declaration for the action inside your config.routes.rb file. Read here for more information on routing and paths.
I have created a route like this:
match "/help" => redirect("/faq"), :as => stackoverflow_help
I am trying to add an anchor tag to the url to go to a particular section in the faq page. For e.g.
link_to "Faq", stackoverflow_help_path(:anchor =>"etiquette")
is just getting redirected to "http://stackoverflow.com/faq" but loosing the "etiquette" anchor. How can I make it go to "http://stackoverflow.com/faq#etiquette"?
If all you need to do is add #etiquette to the end of the url, you could do this:
link_to "Faq", stackoverflow_help_path + "#etiquette"
All thats doing is appending the anchor to the end, but that should be just fine for what you're trying to do.
I'm creating in my index page of my ruby on rails program, a list of the most commonly searched for terms in my database and hence each time a user selects a specific category this is written to another database.
What i would like it to create a hyperlink and pass a certain amount of parameters to a form like is usually done with a select_tag but instead with just a hyperlink, i would like to pass a set of hidden fields that i have on the page as well as what the user has selected.
To give you a better idea, basically i have the following structure in my program:
User inputs a search on (index.html.erb), user clicks on submit tag
action, user is taken to search.html.erb page and is displayed a set of refined categories + some fields, submit button,
user is taken to closest.html.erb (which uses parameters from the previous form by invoking the params[:searchSelected] and a few other params. )
I would also like to add this functionality:
Mimick this same operation, but instead of going in the search.html.erb, i would click on an already refined search category on the index.html.erb page (from a link_to , transmit as parameters which link_to the user has chosen + the hidden fields.
i Currently have this code
#stats.each do
|scr|%>
<%= link_to scr.category, :action => 'closest', :id => scr.category%>
I'm not sure if this is relevant, but i currently have the following routes in my routes.rb file
map.resources :stores, :collection => { :search => :get }
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
would anyone please assist me please? this is my first ruby on rails project and i would really like to find a way around this please
I am not sure if this is what you were thinking, but you can add additional parameters to the link_to tag. They are then available in your controller. So:
<%= link_to scr.category, :action => 'closest', :id => scr.category, :other_param => "test" %>
Will be available in your controller.
def closest
params[:other_param] == "test" #this will be true
end
i managed to resolve this by taking the params[:id] and then according to the value either set my own values (instead of the hidden ones in the index.erb which i had set manually anyway) and otherwise, continue as usual had i placed a regular search
View:
<%= link_to obj.ptc_devicename ,"/wiuconfig/hd?idval=#{obj.id.to_s}&val=#{#devicetype}",:value => obj.ptc_devicename,:id =>obj.id %><br/>
Controller:
#Heading= params[:val]
#id=params[:id]
value will be id is 2 and val is #devicetype