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
Related
So I am working on this rails project. I am trying to get an input from the view and pass it into the controller, so it can be saved into a variable for later use.
I have my view layer in /views/welcome/index.html.erb with the following :
<div class="locator-input">
<%= text_field_tag :user_input %>
<%= submit_tag "Get started" %>
</div>
and the controller in /controller/welcome_controller.rb
class WelcomeController < ApplicationController
def index
end
def locator
#user_input = params['user_input']
end
end
and routes.rb here
Rails.application.routes.draw do
root 'welcome#index'
end
so when I go ahead and hit submit, I get this error:
I know this error has something to do with routes.rb but I can't figure out how to fix it.
My ultimate goal is to get user input, pass it into the controller and save it into a variable to I can use it with my model later on.
I will greatly appreciate some guidance from our experts.
Thanks.
The route generate with root expects only GET requests. But your form sends a POST request.
To solve this just add the following to your config/routes.rb:
root 'welcome#index'
match '/', to: 'welcome#index', via: [:post]
Wrap the field in a form
<div class="locator-input">
<%= form_tag locator_welcome_path %>
<%= text_field_tag :user_input %>
<%= submit_tag "Get started" %>
<% end %>
</div>
You also need to create a route
Rails.application.routes.draw do
resource :welcome, controller: :welcome do
post :locator
end
root 'welcome#index'
end
This will generate
locator_welcome POST /welcome/locator(.:format) welcome#locator
I'm fairly new to rails and didin't find a soloution after a lot of research...
I want to build a simple data abstaraction test app. I have a text field and a submit button and I want to do something with the input (like ie. .upcase ) and then print it out. On submit, I get a routing error, though.
What am I doing wrong?
application.html.erb
<body>
<%= form_tag("parse_input", method: "post") do %>
<%= text_field(:ans, :field) %>
<%= submit_tag("submit" %>
<% end %>
FormControllerController.rb
class FormControllerController < ApplicationController
def parse_input
params[:ans].each do |value|
puts value
end
end
end
routes.rb
Rails.application.routes.draw do
...
root :to => 'application#home'
get "application" => "form_controller_controller"
end
I don't want to use a DB btw.
Try below code:
routes.rb
Rails.application.routes.draw do
...
root :to => 'application#home'
post "parse_input" => "form_controller_controller#parse_input"
end
application.html.erb
<body>
<%= form_tag("/parse_input") do %>
<%= text_field(:ans, :field) %>
<%= submit_tag("submit") %>
<% end %>
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.
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 %>