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
Related
I have a scope 'games-posts' in my Post model that fetches posts that belong to a Topic with a title "Games" (Post belongs_to Topic). In my routes I have defined the route get 'games-posts', to: 'topics#games' and have the view where I show the list of those games posts. In each of the posts I have a link that takes me to that specific games post.
Apart from that I have regular resources :posts routes that the generate standard urls:
`/posts`
`/posts/:id`
`/posts/:id/edit`
etc.
My problem is that when I click on a specific post on games-posts page right now it redirects me to /posts/1 (for example).
It would be cool if it redirected me to /games-posts/1 instead of /posts/1 and similarily I would like to edit and destroy those posts from games-posts page. How can I accomplish this? Is it possible to define something like
resources :games-posts?
The :controller option lets you explicitly specify a controller to use for the resource. For example:
resources :games_posts, controller: 'posts'
Now run rake routes and you will get following output to verify if it generated your required paths.
games_posts GET /games_posts(.:format) posts#index
POST /games_posts(.:format) posts#create
new_games_post GET /games_posts/new(.:format) posts#new
edit_games_post GET /games_posts/:id/edit(.:format) posts#edit
games_post GET /games_posts/:id(.:format) posts#show
PATCH /games_posts/:id(.:format) posts#update
PUT /games_posts/:id(.:format) posts#update
DELETE /games_posts/:id(.:format) posts#destroy
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 two controllers offers and posts.
In routes.rb I've got the following...
resources :offers
get "/posts" => "posts#index"
post "/posts" => "posts#create"
get "/posts/new" => "posts#new"
get "/posts/:id/edit" => "posts#edit"
get "/posts/:id" => "posts#show"
put "/posts/:id" => "posts#update"
patch "/posts/:id" => "posts#update"
delete "/posts/:id" => "posts#destroy"
It was my understanding that these two ways of doing the routing are identical in their operation. Or put another way resources :offers is a just shortcut for writing out each route.
My problem however is when I do a rake routes I get...
Prefix Verb URI Pattern Controller#Action
offers GET /offers(.:format) offers#index
POST /offers(.:format) offers#create
new_offer GET /offers/new(.:format) offers#new
edit_offer GET /offers/:id/edit(.:format) offers#edit
offer GET /offers/:id(.:format) offers#show
PATCH /offers/:id(.:format) offers#update
PUT /offers/:id(.:format) offers#update
DELETE /offers/:id(.:format) offers#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
posts_new GET /posts/new(.:format) posts#new
GET /posts/:id/edit(.:format) posts#edit
GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
PATCH /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
The 'shorthand' offers routes have a four prefixes assigned to them, whereas the longhand posts routes have only two.
So my questions are:
Why don't all the routes get given prefixes when written out longhand?
Is there any way of assigning a prefix to a route when writing them out longhand?
You can assign a prefix to a route when writing them out longhand by doing:
root 'pages#index', as: :home, where :home is your prefix.
Source:
Generating Paths and URLs from Code
I'm struggling with this line in the _form: <%= simple_form_for(#post, url: blog_path) do |f| %>, which gives me the error:
No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id]
Keep in mind that in my routes I have: resources :blog, controller: 'posts', which is to say I am working off of a posts MVC, but I wanted /posts/ to be replaced by /blog/ in the routes.
posts_controller
def new
#post = Post.new
end
def edit
end
The _form works when I go to edit, but not create new.
routes
blog_index GET /blog(.:format) posts#index
POST /blog(.:format) posts#create
new_blog GET /blog/new(.:format) posts#new
edit_blog GET /blog/:id/edit(.:format) posts#edit
blog GET /blog/:id(.:format) posts#show
PATCH /blog/:id(.:format) posts#update
PUT /blog/:id(.:format) posts#update
DELETE /blog/:id(.:format) posts#destroy
model_path by default routing logic in Rails leads to blog#show => /blogs/:id
Change it to blogs_path.
Looking at you routes, I see obvious naming conflict, you must be defined routes wrong.
Be sure it looks like resources :posts, :as=>"blogs", both plural.
UPD
If you want to have only one blog, then resource :post, :as=>"blog", both singular.
But that means one actual input. I'm quite sure you speak of blog/post1, blog/post2, otherwise I can't see any sense in calling it blog?
sorry my english...
<%= simple_form_for(#post, url: blog_path) do |f| %> In this line you are redirecting the form to show (blog_path)
But , depending on the route , the show needs an id (blog GET /blog/:id(.:format)).
You must create a "create" method in the controller that receives the parameters strong ...
There is another method you can use: rails generate scaffold_controller posts
This will build the full CRUD , so you only have to configure the parameters
I hope that helps you
I got best of both worlds this way:
resources :blog, to: 'posts'
resources :posts
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.