I really have no clue why it happen.
this is my routes
resources :users do
resources :bookmarks
end
Controller
# bookmarks_controller
def edit
# this returns perfectly data in the edit view
#bookmark = current_user.bookmarks.find(params[:id])
end
This is the view
<%= form_for #bookmark do |b| %>
<% end %>
Since everything messed up when I nested the bookmarks resources inside users it causes the error
undefined method `bookmark_path'
Then I change the form to
<%= form_for user_bookmark_path(current_user, #bookmark) do |b| %>
The error is gone but there is no such data in the text field form, and the form action is /users/[user_id]/bookmarks/[bookmark_id]/edit
rake routes info
user_bookmarks GET /users/:user_id/bookmarks(.:format) bookmarks#index
POST /users/:user_id/bookmarks(.:format) bookmarks#create
new_user_bookmark GET /users/:user_id/bookmarks/new(.:format) bookmarks#new
edit_user_bookmark GET /users/:user_id/bookmarks/:id/edit(.:format) bookmarks#edit
user_bookmark GET /users/:user_id/bookmarks/:id(.:format) bookmarks#show
PUT /users/:user_id/bookmarks/:id(.:format) bookmarks#update
DELETE /users/:user_id/bookmarks/:id(.:format) bookmarks#destroy
Any idea how to fix it ? Thanks
Try something like:
<%= form_for [current_user, #bookmark] do |b| %>
<% end %>
Related
I have the following routes on my project:
namespace :teacher do
resources job_applications do
resources :job_application_addresses
end
My form has the folling code
<%= simple_form_for [:teacher, #job_application_address] do |form|
<% end %>
And my controller has the following:
def new
#job_application_address = JobApplicationAddress.new
end
def create
#job_application_address = JobApplicationAddress.new(job_application_address_params)
#job_application_address.job_application = #job_application
if #job_application_address.save
flash[:success] = 'Successfully created'
end
end
Finally I'm getting this error:
undefined method `teacher_job_application_addresses_path' for #<#<Class:0x00007fda0c4191d0>:0x00007fda143d1af8>
Did you mean? teacher_job_application_path
teacher_job_applications_path
Extracted source (around line #3):
<%= simple_form_for [:teacher, #job_application_address] do |form| %>
This are my routes for this view:
teacher_job_application_job_application_addresses GET /teacher/job_applications/:job_application_id/job_application_addresses(.:format) teacher/job_application_addresses#index
POST /teacher/job_applications/:job_application_id/job_application_addresses(.:format) teacher/job_application_addresses#create
new_teacher_job_application_job_application_address GET /teacher/job_applications/:job_application_id/job_application_addresses/new(.:format) teacher/job_application_addresses#new
edit_teacher_job_application_job_application_address GET /teacher/job_applications/:job_application_id/job_application_addresses/:id/edit(.:format) teacher/job_application_addresses#edit
teacher_job_application_job_application_address GET /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format) teacher/job_application_addresses#show
PATCH /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format) teacher/job_application_addresses#update
PUT /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format) teacher/job_application_addresses#update
DELETE /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format) teacher/job_application_addresses#destroy
What should I do? I'll apprecciate your help.
Your job_application_addresses is nested resource of job_application. But i dont see it in your form.
You can do it by 2 ways:
Your can add job_application object in the simple_form_for url builder like this
<%= simple_form_for [:teacher, #job_application, #job_application_address] do |form|
<% end %>
You can add an url option in the form
<%= simple_form_for [#job_application, #job_application_address], url: teacher_job_application_job_application_addresses_path do |form|
<% end %>`
In both cases you need to have #job_application object in new action in controller.
Hope it helps
I have nested resources and I'm trying to show the new layout for the nested resource on the show of the parent.
resources :discussions do
resources :comments
end
discussions\show.html.erb
<%= #discussion.title %>
<%= ... render the discussion %>
<%= ... render the existing comments %>
<% render 'comments/new' %> <--- trying something like this
comments/new throws an error because it's missing the partial.
comments/form works to get past that, but throws an error saying my #comment is nil.
comments/_form.html.erb
undefined method discussion for nil:NilClass
<%= bootstrap_form_for([ #comment.discussion, #comment] ) do |f| %>
Do I have to change something in the controller, or am I going about this incorrectly?
Thanks for your help!
try this
discussions\show.html.erb
<%= render 'comments/form', comment: #discussion.comments.build %>
comments/_form.html.erb
<%= bootstrap_form_for([ comment.discussion, comment] ) do |f| %>
Hope this will work.
i am a newbie to rail and try to build my first site but face an issue with a link_to in an index page. The link redirect to /recipes.1 instead of /recipes/1.
The show page work when i try /recipes/1.
Index.html.erb
<% provide(:title, "Recipe") %>
<% #recipes.each do |recipe| %>
<%= link_to recipe.label, recipe%>
<%end%>
route.db
get 'recipe' => 'recipes#show'
get 'recipe' => 'recipes#new'
post 'recipe' => 'recipes#create'
resources :users
resources :recipes
recipes_controller.rb
def index
#recipes = Recipe.all
end
def show
#recipe = Recipe.find(params[:id])
end
Remove the following routes from routes.rb:
get 'recipe' => 'recipes#show'
get 'recipe' => 'recipes#new'
post 'recipe' => 'recipes#create'
The above routes are not required since resources :recipes generate all these routes for you.
Hope it helps!
Try this code
<% #recipes.each do |recipe| %>
<%= link_to recipe.label, recipe_url(recipe) %>
<%end%>
Use
<%=link_to recipe.label, recipe_path(recipe)%>
Instead of
<%= link_to recipe.label, recipe%>
you have defined show path twice.
1. in manual defined path
get 'recipe' => 'recipes#show'
2. Through
resources :recipes
If you do rake routes, then you will first option create routes for get method with /recipe url and appends parameter to it which result in /recipes.1 path.
Also rails read routes file in top to bottom approach. As uses first routes for show method.
This error is creeping in on more than one occasion and i can't seem to pin point it.
error log:
undefined method `medium_path' for #<#<Class:0x0000010687a788>:0x00000101d62d90>
Extracted source (around line #3):
media controller.
class MediaController < ApplicationController
def index
#medias = Media.all
end
def show
#media = Media.find(params[:id])
end
def edit
#media = Media.find(params[:id])
end
end
edit.html.erb.
<h1>Editing <%= #media.title %></h1>
<%= form_for(#media) do |f| %>
<p>
<%= f.label :title %>
</p>
<% end %>
routes.rb
Mediastuff::Application.routes.draw do
root "media#index"
get "media" => "media#index"
get "media/:id" => "media#show", as: "show_media"
get "media/:id/edit" => "media#edit", as: "edit_media"
end
I believe that error is generated from your form_for declaration. In addition to what you already have in your config/routes.rb, you may also want to add a route for update action as that form_for(#media) is going to be an update.
Add the following to your config/routes.rb:
put "media/:id/update" => "media#update"
Also make sure to define update action in your MediaController.
Another option would be to use resources in config/routes.rb as a replacement to all the media/... routes you have:
Mediastuff::Application.routes.draw do
root "media#index"
resources :media
end
And to see what path/url helpers you can use, run rake routes from terminal.
Noob here.
Trying to figure out how to display a method in my controller into my index page. Here is what I have thus far.
Controller -
class SammichesController < ApplicationController
def index
#sammiches = Sammich.all
end
def create
#sammich = Sammich.find_by_name(params[:sammich][:name])
end
def random
#sammichy = Sammich.rand(params[:sammich][:name])
end
end
Routes-
Sammiches::Application.routes.draw do
resources :sammiches do
get "random"
end
root :to => 'sammiches#index'
Index-
<h1>All my Sammiches</h1>
<%= form_for Sammich.new do |f| %>
<%= f.label :sammich %>
<%= f.text_field :name %>
<%= f.submit 'Find Sammich', :id => 'sammich_submit' %>
<% end %>
<%= link_to "Random sandwich", sammich_random_path %>
routes-
sammich_random GET /sammiches/:sammich_id/random(.:format) sammiches#random
sammiches GET /sammiches(.:format) sammiches#index
POST /sammiches(.:format) sammiches#create
new_sammich GET /sammiches/new(.:format) sammiches#new
edit_sammich GET /sammiches/:id/edit(.:format) sammiches#edit
sammich GET /sammiches/:id(.:format) sammiches#show
PUT /sammiches/:id(.:format) sammiches#update
DELETE /sammiches/:id(.:format) sammiches#destroy
root / sammiches#index
Error-
localhost:3000 - Routing Error
No route matches {:action=>"random", :controller=>"sammiches"}
Try running rake routes for more information on available routes.
Any assistance would be much appreciated.
Thanks!
If you look at your route it has :sammic_id in there as well:
sammich_random GET /sammiches/:sammich_id/random(.:format) sammiches#random
Which means you need to pass an id to your URL helper sammich_random_path which you haven't.
Update your routes to this:
resources :sammiches do
collection do
get "random"
end
end
After adding that your route would be just /sammiches/random