I created a new route and a corresponding method in a controller and view:
routes.rb:
get "pictures/user/:username" => 'Pictures#user'
pictures_controller.rb:
def user
#pictures_user = User.where(username: params[:username]).first
render 'user.html.erb'
end
user.html.erb:
Just some simple html/erb
I get this following error. I know the user method is being entered, seems to be a problem rendering the view?
Routing Error
No route matches {:controller=>"pictures", :action=>"user", :username=>"my_username", :page=>nil}
What am I missing?
user.html.erb is inside the same directory of views/pictures? just do
render 'user'
should be fine i think
and I guess your Pictures downcase will be better ##
get "pictures/user/:username" => 'pictures#user'
Related
I have Rails 5 installed. I'm having trouble getting my show page to render. My rake routes has this
votes_show GET /votes/show(.:format) votes#show
Then in my app/controllers/votes_controller.rb file I have
def show
id = params[:id]
# If there is no person selected based on the param, choose one randomly
if !id.present?
#person = Person.order("RANDOM()").limit(1).first
else
#person = Person.find(:id => params[:id])
end
but when I go to http://localhost:3000/votes/1, I get a
No route matches [GET] "/votes/1"
error. I'm missing something real obvious. What is it?
votes_show GET /votes/show(.:format) votes#show
this line is telling rails that "when you see a route that is /votes/show then go to the VotesController#show method."
When you then type in /votes/1, rails is checking that against the routes and not finding a route that looks like that... it is specifically looking only for /votes/show
As mentioned in the comments, you must add a line like:
get '/votes/:id(.:format)', to: 'votes#show'
or better yet, use resourceful routes like
resources :votes
This is what my routes currently look like:
which gives
On my homepage I have a create vacancy button
<%= link_to "plaats", new_employer_vacancy_path(:employer_id)%>
Which should be linked to the line from the first image
get '/employers/:employer_id/vacancies/new', to: 'vacancies#new', as: 'new_employer_vacancy'
In the vacancies_controller#new - create I have:
def new
#vacancy = Vacancy.new
#employervacancy = Employervacancy.new
end
def create
#vacancy = Vacancy.create(vacancy_params)
createEmployervacancy
redirect_to employer_vacancy_path(current_employer, #vacancy)
end
def createEmployervacancy
#employer = current_employer
Employervacancy.create(vacancy_id: #vacancy.id, employer_id: #employer.id)
end
But whenever I click the button I get redirected to some other method in my vacancies_controller that is totally irrelevant.
How is this even possible? Don't I clearly define that when that path is clicked he should go to vacancies#new? and not to vacancies#show_specific_employer_vacancies?
EDIT
After following the answers I am indeed being linked to the correct route.
First, it gave me this error.
After trying to pass the current_employer.id instead of #employer like suggested I got following error:
For your routes, you'd better to change into nested route for easily maintaining routes.
Remove these codes:
get '/employers/:employer_id/vacancies/:id', to:"vacancies#show_specific_employer_vacancies", as: "employer_vacancy"
get '/employers/:employer_id/vacancies/edit/:id' ...
get '/employers/:employer_id/vacancies/index' ...
get '/employers/:employer_id/vacancies/new' ...
path '/employers/:employer_id/vacancies/:id' ...
change into:
resources :employers do
resources :vacancies
end
Try to use basic routes here because you use standard simple form url. For example:
<%= simple_form_for(#employee, #vacancy) %>
The simple_form_for will generate url well if you use nested routes above.
Finally, in your link you have to add #employer_id
<%= link_to "plaats", new_employer_vacancy_path(:employer_id => #employer_id)%>
I hope this help you
Your router cannot tell the difference between your employer_vacancy and new_emplyer_vacancy routes because the :id parameter accepts anything. Because of this, when you point your browser to "/employers/5/vacancies/new", the route is taking your employer_vacancy route and assigning {:employer_id => 5, :id => "new"} instead of going to your new_employer_vacancy route (because routes are first-come-first-serve).
To correct this, add a constraint to your first route to ensure that only numbers (and not the string "new") is accepted into the employer_vacancy route:
get '/employers/:employer_id/vacancies/:id',
to: 'vacancies#show_specific_emplyer_vacancies',
as: 'employer_vacancy',
constraints: { id: /\d+/ } # <- This line
As Wes Foster said rails router is trying to find a first match.
It means that given a path /employers/999/vacancies/new your router looks through the routes and when it sees get '/employers/:employer_id/vacancies/:id he thinks that this route matches. So :employer_id is 999 and :id is new.
I'd suggest to put the route with :id at the end of employers routes:
...
get '/employers/:employer_id/vacancies/new'
...
get '/employers/:employer_id/vacancies/:id'
Btw this is better than adding a constraint because:
It is easier
It doesn't pollute routes file
Later you may want to change ids to be hashed or alphabetic and then you'd have to change the constraint
I have a custom route called "work_path" and when I try to access it I get this error
"Missing template portfolios/show"
I am not trying to access the "show" view, but rather one named "work" (work.html.erb). I have no idea why it keeps trying to get me to the "show view"
My custom route
get 'portfolios/work' => 'portfolios#work', :as => :work
Portolios Controller
def work
#portfolio = #portfolio.active
end
The link I'm using:
<%= link_to "Work", work_path %>
I don't know why it's trying to redirect me.
You probably have also resources :portfolios in routes.rb above your custom route and that causes your problem. Switch these lines' positions.
I have a controller and a view (ctrler)
controller
def index
...
end
def show
#text = params[:text]
end
end
View (show.html.erb)
<%=#text %>
routes.rb
resources :ctrler
match 'ctrler/:text' => 'ctrler#show'
If I fire the rails s server up and load up http://localhost:3000/ctrler/hiiiiiii I get nothing but if I load http://localhost:3000/ctrler?text=hiiiiii I get text!
Im still trying to get the hang of rails I'm used toPHP but can someone give me some guidance here am I on the right track or have I missed something out?
resources :ctrler
creates the following rule
match "ctrler/:id" => "ctrler#show"
This route conflicts with
match 'ctrler/:text' => 'ctrler#show'
In the event of a conflict, the rule that appears first takes precedence, so when you go to 'ctrlr/hiiiii', it is setting the id parameter to hiiiii, not the text parameter. Try to change routes.rb to
match 'ctrler/:text' => 'ctrler#show'
resources :ctrler
and see if that helps.
In my view I specify a named route like this:
show.html.erb ->
seo_path(:id => "45")
Now in my routes I define like so:
routes.rb ->
map.seo "/pages/:id/:permalink", :controller => "pages", :action => "show"
Below is the error message I am getting. Apparently the diff is the id, although I don't know why.
Update:
I am getting this as my error:
seo_url failed to generate from {:controller=>"pages", :id=>"45", :action=>"show"}, expected: {:controller=>"pages", :action=>"show"}, diff: {:id=>"45"}
Why not using the to_param method?
class YourModel << AR::Base
def to_param
"#{id}-#{title.parameterize}"
end
Thus getting routes like
/pages/1-here-is-my-converted-article-title-to-permalink
Besides that, it would seems Rails isn't recognizing your route. Have you restarted your web server? Try sending only a :permalink attribute to see how it goes.
I don't fully understand your question.
It looks like id is already specified in your named route, and in your view. It looks like you need to also specify permalink in your view. Are you getting an error?