link_to Base URL Randomly Changed - ruby-on-rails

I'm using link_to to create a custom link. For some reason, my url always is wrong and randomly adds blog to the URL. I can't figure out why. It only does this while within the partial that I am rendering (3 times on the page).
I can share any code necessary, but where the hell would it randomly grab the blog URL?
The browser renders:
Select Rate
My view (using slim) is:
- for rate in loantek_rates
- loantek_closing_costs = LoantekClosingCosts.new(rate)
= link_to 'Select Rate', {controller: 'quote'}
Routes
#routes.rb
match "/blog" => redirect("http://blog.mywebsite.com"), path: '/blog'
resources :quotes, path: 'quote'

Set paths in link_to tag which you can get from rake_routes
Ex.
link_to "Profile", profile_path(#profile)

Related

rails named route puts '.' instead of '/' in URL on updating [duplicate]

This question already has an answer here:
Rails dot instead of slash in URL
(1 answer)
Closed 5 years ago.
Update #3 ... fixed! Solution was moving the new name space code (get 'dash', to: 'dashes#show') to the bottom of the routes.rb file just above the root "campaigns#index" entry.
Update #2 ... it's not a pluralization as it's called singularly, shown below & removing the named route automatically corrects all issues.
Update #1 ... found reason why no one had listed period in place of slash in URL ... it's unix nouns, not english ... IE: if you search for 'dot' in place of '.', then you get all sorts of answers.
I'm flumoxed by this one ... made my first named route the other day, everything looked great until suddenly it appears that the edit page when called from the named route doesn't update properly, with error ...
No route matches [PATCH] "/dash.6"
Removal of the named route takes me back to normal routes & all options working. I can't find a mention of routing which uses '.' instead of '\', so I'm lost. My route file below & then the route results from rails server ...
Rails.application.routes.draw do
devise_for :users, controllers: { sessions: 'users/registrations' }
# map.login '/login', :controller => 'sessions', :action => 'new' ## 3rd try
# get '/dash', :controller => 'dashes', :action => 'show' ## 2nd try
# get 'dash', to: 'dashes#show' ## Original named route
resources :dashes
resources :campaigns
resources :players
resources :countries
root "campaigns#index"
# yank later
resources :neighborhoods
end
Rails results on server ...
Paths Matching (dashes):
dashes_path GET /dashes(.:format)
dashes#index
POST /dashes(.:format)
dashes#create
Paths Containing (dashes):
dashes_path GET /dashes(.:format)
dashes#index
POST /dashes(.:format)
dashes#create
new_dash_path GET /dashes/new(.:format)
dashes#new
edit_dash_path GET /dashes/:id/edit(.:format)
dashes#edit
GET /dashes/:id(.:format)
dashes#show
PATCH /dashes/:id(.:format)
dashes#update
PUT /dashes/:id(.:format)
dashes#update
DELETE /dashes/:id(.:format)
dashes#destroy
My form is standard rails generated & works whenever I remove the named route ... edit.html.haml renders the _form partial ...
%h1 Editing dash
= render 'form'
= link_to 'Show', #dash
\|
= link_to 'Back', dashes_path
_form.html.haml
= simple_form_for(#dash) do |f|
= f.error_notification
.form-inputs
= f.input :name
= f.association :user
= f.association :dashcampaigns
= f.association :dashplayers
.form-actions
= f.button :submit
As described in this question, typically the issue is when you confuse between collection (plural) and member (singular) controller actions.
A collection controller action is an action that does not have an ID since it does not manipulate an existing resource (dash) or since it works on a group of resources. The RESTful collection actions are: index, new, and create. Each of them has a helper method and a "verb" (method):
index: url: dashes_path, method: :get (method get is default)
create: url: dashes_path, method: :post
new: url: new_dash_path, method: :get
Note how both index and create share the same plural URL helper method dashes_path. Only the method option differentiates between them.
A member controller action is an action that has an ID of the resource it is manipulating (one particular dash). The RESTful collection actions are:
edit: url: edit_dash_path(#dash), method: :get
show: url: dash_path(#dash), method: :get
update: url: dash_path(#dash), method: :patch
destroy: url: dash_path(#dash), method: :destroy
See how except edit, all other actions use the same singular URL helper method dash_path(#dash).
You can find further details on this in the guides.
Now, the "dot instead of slash" symptom is when you mistakenly try to point to a member action in a way that should be used for collection actions. So if you try to do dashes_path(#dash) - there is no such thing. The only parameter dashes_path accepts is format, which is added to the URL in the end after a dot, that's why you're seeing a weird URL such as /dash.6.
Rails form builder form_for and extensions such as simple_form_for need to decide whether to point the form action at the #create collection action (when rendering the #new collection action) or #update member action (when rendering the #edit member action). They do so "magically" by taking a look at the object you give them when you do simple_form_for(#dash). If #dash.new_object? is true, they point the form action to #create, if it's false they point it to #update.
In your case when you used it "out of the box" it was all good. It started acting up on you when you added this line to your routes.rb:
get 'dash', to: 'dashes#show'
By default show is a member action, not a collection action. It must receive an ID. I think that this is why Simple Form is acting up on you. Instead of that alias, try this one:
get 'dash/:id', to: 'dashes#show'
Let us know if this fixes the issue.
And in general, I recommend to "work with" the RESTful routing rather then "working against" them. It is very rare to need to add named routes in routes.rb. There are exceptions, but I don't think it is justified in your case to deviate from conventions and try to use dash/1 rather than dashes/1. "Working with" Rails will get you the productivity boost it is known for, not "working against" it by trying to force it for relatively small details like this one.

basic routing - how to create a link_to path from match

Ok, say you have this:
match "tutor_appointments/new_appt" => "tutor_appointments#new_appt"
How do I create a link_to path from it?
Would it be something like this: (it doesn't work, btw)
<%= link_to "New Appointments", controller:tutor_appointments, method:new_appt %>
I'm always confused on routing stuff when it comes to figuring out how to do link_to link.
I do understand that tutor_appointments is the controller and new_appt is the method.
Ideally, you would name the route:
http://guides.rubyonrails.org/routing.html#naming-routes
And then you can refer to the route by that name.
eg. if you had:
match "tutor_appointments/new_appt" => "tutor_appointments#new_appt", as: 'new_appointment'
Then you could do:
link_to 'New Appointments', new_appointment_path
However, in this case it sounds like what you actually want is resource routing:
http://guides.rubyonrails.org/routing.html#resources-on-the-web
And you want a 'new' action for your 'tutor_appointments' resource.

Finding a Good Ruby on Rails Tutorial

I am trying to link the index page from my movies model to the index page of my random generator model. I am using Ruby version 1.9.2 This is what the top few lines of the movie index view looks like:
This is the top two lines of my index view
%h1 Topical Memory System
= link_to "View Random Generators", randomgenerator_path
and this is what my routes file looks like:
Rottenpotatoes::Application.routes.draw do
resources :movies
resources :randomgenerators
# map '/' to be a redirect to '/movies'
root :to => redirect('/movies')
end
When I try to run the app on WEBrick, it throws an exception at those top lines and says the the route for {:action => "show",
:controller => "randomgenerators" } does not exist when rake routes says it does. What am I doing wrong?
Your label says "view random generators". That sounds to me like a list of all the generators, in which case your link should actually be:
= link_to "View Random Generators", randomgenerators_path
Notice above that I made randgenerators plural. If you actually meant to go to the show action, than you need to provide an :id for which randomgenerator it is that you want to see:
= link_to "View Random Generators", randomgenerator_path(whatever the id is you're trying to get)

No route matches [POST] "/student_tests/upload.1"

But I wanted it to match [POST] "/student_tests/1/upload"
IN The routes.rb i have added
resources :student_tests do
member do
post 'upload'
end
end
In the students_test_controller i have added
def upload
render :upload
end
In the index page of student_test
<%= link_to "| Upload Scaled Scores", upload_student_test_path(test), method: :edit%>
where test is an object of student_test
By using Rack routes
upload_student_test POST /student_tests/:id/upload(.:format) student_tests#upload
What mistake have i made that its searching for "/student_tests/upload.1" i want it to look for "/student_tests/1/upload" (here 1 is the id of student test)
You've created an 'POST' member action in your route but you've linked to it in wrong way:
<%= link_to "| Upload Scaled Scores", upload_student_test_path(test), method: :edit%>
which should actually be like:
<%= link_to "| Upload Scaled Scores", upload_student_test_path(test), method: :post %>
However, this may not be conventional way of rails routing. If you just want to render a views there, a GET method will be most appropriate. When you upload file(s), you can use the POST method as you tried here.
I think the way you used your route could be changed as follows:
match ':controller(/:action(/:id(.:format)))'
I don't understand the reason why you want the ID before the controller's action. You can change the routes format and access the ID accordingly.
The way you have framed the question could have been better as I found hard to understand your question. Answering here with what I understand.

Does link_to automatically pass :id when controller is specified?

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.

Resources