I'm trying to follow this guide:
http://guides.rubyonrails.org/getting_started.html#hello-rails-bang
and in section 5.7 it says to add this line.
post GET /posts/:id(.:format) posts#show
What does this do, and where should I put it? I tried putting it in the "create" method that follows this form:
http://dixonc3-72812.use1.nitrousbox.com/posts/new
I also tried putting it in the "view" because I figured it was accepting the "POST" method. How do I proceed? New to Ruby on Rails and trying to figure out the kinks.
I'm trying to follow this guide:
http://guides.rubyonrails.org/getting_started.html#hello-rails-bang
and in section 5.7 it says to add this line.
post GET /posts/:id(.:format) posts#show
Section 5.7 doesn't tell you that. What it tells you is the following is output of rake routes for the show action:
post GET /posts/:id(.:format) posts#show
The output is presented in a tabular format where the four columns are (from left to right): Prefix, Verb, URI Pattern, Controller#Action.
In order to get this, you need to declare posts as a resource in config/routes.rb as:
resources :posts, only: [:show]
Now if you run rake routes in your terminal, you will see the line included from the guide.
The above line in config/routes.rb defines a route for the show action. You can remove only: [ :show ] option to have routes defined for all standard RESTful actions, i.e. in your config/routes.rb:
resources :posts
This is the routes for show action of posts controller, for create action you find something like
posts#create
Related
I'm learning rails 6 and walking through creating some toy resources. I know in my data model, I want to create a "widget" let's say, but I don't want everything that a scaffold is going to produce, so I'd like to manually generate everything that I know I'll need and nothing more. I started with a simple model. Next I'm trying to generate the controller + views I know I'll need. I ran:
rails g controller widgets index new show delete
This ended up creating a router with the following routes:
get 'widgets/index'
get 'widgets/new'
get 'widgets/show'
get 'widgets/delete'
I'm surprised by this, I would have expected something similar to the routes that are generated by default with the scaffold:
things GET /things(.:format)
POST /things(.:format)
new_thing GET /things/new(.:format)
edit_thing GET /things/:id/edit(.:format)
thing GET /things/:id(.:format)
PATCH /things/:id(.:format)
PUT /things/:id(.:format)
DELETE /things/:id(.:format)
Am I using the correct generator command to accomplish what I described? Or is this something that I would just need to manually set up with my routes?
EDIT: I realized the action I'm actually looking for is :destroy, and not :delete
This is normal if you don't want to use scaffold, and for some routes they are need to be setup manually like the delete route, it is a delete request not get like the controller is generating (the controller always generate get requests because it expects a corresponding view)
You can use: resources :widgets, only: [:index, :new, :show, :destroy]
this will generate what you want because this is the convention used in rails
The Rails generators are really just for generating the needed files, for anything out of the ordinary you will have to manually change things.
You can add this to your routes.rb instead of that list of GETs
resources :widgets, only: [:index, :new, :show, :destroy]
Which will produce an output similar to what you have in your question if you do rails routes from the terminal.
I got my ruby on rails without routes like posts, new, show, edit, create? See code below:
C:\BBJ\myrubyblog2> rake routes
Prefix Verb URI Pattern Controller#Action
home_index GET /home/index(.:format) home#index
root GET / home#index
What does it mean? I don't have any show, posts, new, edit routes, because I didn't create them or are they missing on my rails app folder?
Is there any command that I can run to create each of them?
I think you are looking for resource routing. Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code
resources :posts
creates seven different routes in your application, all mapping to the posts controller:
HTTP Verb Path Controller#Action Used for
GET /posts posts#index display a list of all posts
GET /posts/new posts#new return an HTML form for creating a new post
POST /posts posts#create create a new post
GET /posts/:id posts#show display a specific post
GET /posts/:id/edit posts#edit return an HTML form for editing a post
PATCH/PUT /posts/:id posts#update update a specific post
DELETE /posts/:id posts#destroy delete a specific post
add it your routes.rb file and then hit rake routes in terminal
For more details on routing you can follow this guide:
http://guides.rubyonrails.org/routing.html
I have created one controller and added default 7 action methods , I have also created 4 views and those are index , new , show , edit all contains difeent data
also added resources :posts controller configuration into my routing file as well
'
My problem is when i try to navigate to index or edit views using url .. it displays me only show views , however index method is working as a default
below are my urls `enter code here`
1 . http://localhost:3000/posts/index[^]
if i try this it gives me show view data
localhost:3000/posts
this gives me proper index data (default without mentioning index action name)
http://localhost:3000/posts/edit[^]
this one but always shows me data present in show view and not in data present in edit view
Please suggest
Below are few lines from my route file.
Hide Copy Code
Rails.application.routes.draw do
get 'home/index'
resources :posts
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'home#index'
Please read the Rails guide entry on default resource routing, there's nothing unusual going on here.
/posts is how you access your posts#index, not /posts/index.
For show and edit, you need your post id in the URL so that Rails knows which record to retrieve, ie /posts/1/edit
You can run rake routes from the console to see all of your routes. The result of resources :posts should be the following seven routes:
Prefix Verb URI Pattern Controller#Action
POST /posts(.:format) posts#create
new_sheet GET /posts/new(.:format) posts#new
edit_sheet GET /posts/:id/edit(.:format) posts#edit
sheet GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
Rails will match the URL to the URI pattern and take the matching controller action. btw you can use rake routes CONTROLLER=posts to see just the routes for posts.
I am completely new to ruby, andI am following this ruby rails tutorial step by step, except for the detail that I've called my app "cinema".
I created a resource named "posts", there is a controller class called posts controller. From the page posts/new I should post a text with title, and execute an action (show). I am trying to add the show action in the routes file and in the controller class.
The show action should be called when a form is submitted, the form includes a title and a text field, mapped into the database.
In paragraph 5.7, there is a non-clear instruction: it says to add this line:
post GET /posts/:id(.:format) posts#show
To my routes.rb file, but it doesn't say where to write it exactly, I put it under resources:posts (maybe it's the wrong place, it doesn't saying anything about that).
I also added the show method to the controller class:
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title,:test);
end
But when I submit the form I still get this error:
The rake routes command result:
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
It doesn't tell you to add it to routes.rb. It's one of the routes that is created automatically with:
resources :posts
Remove the line from your routes.rb restart the server and continue with the tutorial.
Tip: you can run rake routes to see all available routes in your application.
Your whole question seems completely contradictory to me.
You never do this in rails routes -
post GET /posts/:id(.:format) posts#show
Instead in routes.rb file.
get 'posts/:id' => 'posts#show'
The routing is done on a priority basis (first come first serve), so if your are adding get 'posts/:id' after the resources :posts, it is of no use as resources :posts already does it for you (read the rails routing guide on resources).
Your form submission should be a post data, if you are using resources 'new' should be a 'get' data and the corresponding 'post' should be 'create'. Your 'new' route has an error, then where else are you rendering your form to submit the form data?
My suggestion is that you keep the 'resources :post' and remove every other this corresponding to your :post from routes file. If you have everything else right then it should probably work fine.
resources :posts
delete it
rails s
paste resources :posts
rails s
I make it and success
I created a new controller for one of my models called Review, and named it review_controller, i put in show and update methods in it but cant get them working because rails didn't add the paths for the 2 methods.
I tried putting the follwoing in the routes file: match "/review/update/:id", :to => "review#update"
But it gives me a ActiveRecord::RecordNotFound (Couldn't find Review with ID=update):
app/controllers/review_controller.rb:16:in `update'
how can i add the path to the routes file to make my update and show methods work?
Thank You
For a new controller in Rails 3, you can let Rails build the routes for you like this:
resources :review, :only => [:show, :update]
And then if you were to run rake routes in the Terminal, you'd see:
review GET /review/:id(.:format) {:action=>"show", :controller=>"review"}
PUT /review/:id(.:format) {:action=>"update", :controller=>"review"}