no method error in posts#edit (take 2) - ruby-on-rails

I posted a question before but my pictures weren't posting so now I am just gonna copy and paste the code. I am trying to make a blogging web app is Rails and I am in the "edit" phase and basically I am getting a no method error.
here is my controller code:
class PostsController < ApplicationController
def index
#post = Post.all
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
#post.save
redirect_to show_path(#post)
end
def edit
#post = Post.find(params[:id])
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
and here is my edit.html.erb view page
This is the edit page for
<%= form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %><br />
<%= f.label :body %>:
<%= f.text_field :body %><br />
I am guessing the problem is somewhere in there
here is my routes.rb:
Rails.application.routes.draw do
get "pages/about" => "pages#about"
get "pages/contact" => "pages#contact"
get "/posts" => "posts#index"
post "/posts" => "posts#create"
get "post/:id" => "posts#show", as: :show
get "/posts/new" => "posts#new"
get "post/:id/edit" => "posts#edit", as: :edit_post
end
Please help this is really frustrating :/

You need to add a line in your route file such as:
resources :posts
Also add a update method in your posts_controller:
def update
#post = Post.find(params[:id])
#post.update(post_params)
redirect_to #post
end
I hope this helps you !

When you use form_for like
<%= form_for #post do |f| %>
It's a shorthand for something that looks like this
<%= form_for #post, as: :post, url: post_path(#post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
So form_for expects that you have defined post_path helper, which you get when you use resources to define your routes, like this
resources :photos
Since you haven't defined your routes like that you don't have path helper that form_for expects and you get an error.

here is the error message:
NoMethodError in Posts#edit
Showing /Users/Hisham/Desktop/Rails_projects/myblog/app/views/posts/edit.html.erb where line #6 raised:
undefined method `post_path' for #<#:0x007fcdc84fd7b8>
Did you mean? posts_path
posts_new_path
font_path
Extracted source (around line #6):
4
5
6
7
8
9
<%= form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %><br />
Rails.root: /Users/Hisham/Desktop/Rails_projects/myblog
Application Trace | Framework Trace | Full Trace
app/views/posts/edit.html.erb:6:in `_app_views_posts_edit_html_erb___871381459853082993_70260921890920'

Related

rails: ActionController::RoutingError (No route matches [PATCH] "/article/viewing-article-2"):

I am currently learning rails and facing a totally weird issue.
I am trying to update an existing article by following a tutorial that I am doing, however it is not getting updated. I am receiving the following errors from the terminal and browser respectively:
- Terminal:
* Started PATCH "/article/viewing-article-2" for 127.0.0.1 at 2018-03-28 18:54:46 +0200
* No route matches [PATCH] "/article/viewing-article-2"
- Browser: ActionController::RoutingError (No route matches [PATCH] "/article/viewing-article-2"):
My routes are such as
**routes.erb**
Rails.application.routes.draw do
root to: 'pages#index'
post 'article/create-new-article', to: 'pages#create'
get 'article/viewing-article-:id', to: 'pages#show', as: 'article'
get 'article/:id/edit', to: 'pages#edit', as: 'article_edit'
patch 'article/:id/update', to: 'pages#update', as: 'update_article'
get 'article/new-article', to: 'pages#new'
get 'article/destroy', to: 'pages#destroy'
end
and my controller:
controller.erb
def index
#articles = ##all_articles.all
end
def show
#article = Article.find(params[:id])
end
def edit
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
article_params = params.require(:article).permit(:title, :author, :publisher, :content)
#article.update(article_params)
redirect_to root_path
end
and my html:
edit.html.erb
<% content_for :title do %>Editing <%= #article.title %><% end %>
<% content_for :bodycontent do %>
<h3>Editing <%= #article.title %></h3>
<%= form_for #article do |f| %>
<%= f.text_field :title, class: 'form-control'%>
<%= f.text_field :author, class: 'form-control'%>
<%= f.text_field :publisher, class: 'form-control'%>
<%= f.text_area :content, class: 'form-control'%>
<%= f.submit class: 'form-control btn btn-primary' %>
<% end %>
<% end %>
I am not surely what I am doing wrong, as the selected article does not get updated.
I have loved rails so far, and hope to get better at it. Will appreciate any help.
Modify form_for as follows with custom url
<%= form_for #article, url: #article.new_record? ? article_create_new_article_path : update_article(#post)do |f| %>
but I will suggest you to use Resourceful Routing instead.
At first, you really should using resource routing for you article model:
Rails.application.routes.draw do
root 'pages#index'
resources :articles
end
This is what controller should look like. About permit params read here.
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def edit
#article = Article.find(params[:id])
end
def update
return unless request.patch?
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to root_path
else
render :edit
end
end
private
def article_params
params.require(:article).permit(:title, :author, :publisher, :content)
end

Rails: User form_for for a post does not work

I'm trying to render a text area on a user's profile page to allow them to create a post. At the moment I can view all of a user's posts, but I can't get to their profile page because the form will not render.
I did try to changing some stuff around, but then the form would not post due to a routing error.
I feel like I'm posting to the wrong path but I'm not sure so and advice would be appreciated.
class PostsController < ApplicationController
def index
#posts = Post.all
end
def new
#post = Post.new
end
def create
#post = current_user.posts.build(post_params)
if #post.save
flash[:success] = "Posted!"
redirect_to root_url
else
flash[:notice] = "Post could not be submitted"
redirect_to users_path
end
end
private
def post_params
params.require(:post).permit(:user_id, :content)
end
end
_post_form.html.erb
<%= form_for :post, user_posts_path, {method: "create"} do |f| %>
<%= f.text_area :content, size: "60x12", placeholder: "What do you want to say?" %>
<%= f.submit "Post" %>
<% end %>
There's no such HTTP verb called create, you need to change the method mentioned in your form_for, instead of this:
<%= form_for :post, user_posts_path, {method: "create"} do |f| %>
try this:
<%= form_for :post, user_posts_path, {method: "post"} do |f| %>
Please check rake routes and I think you provided the
resources :users do
resources :posts
end
in routes.eb
so
<%= form_for :post, user_posts_path(#user), html: { method: "post"} do |f| %>
in controller
def new
#post = Post.new
#user = current_user
end

Rails 4 RoutingError: No Route Matches [POST]

I'm working through a small exercise while learning Rails 4, but running into a routing error while trying to update an object. I keep getting an error message: No route matches [POST] "/movies/1/edit" but can't see where my code is not correct:
my movies_controller.rb
class MoviesController < ApplicationController
def index
#movies = Movie.all
end
def show
#movie = Movie.find(params[:id])
end
def new
#movie = Movie.new
end
def create
#movie = Movie.create(movie_params)
if #movie.save
redirect_to "/movies/#{#movie.id}", :notice => "Your movie was saved!"
else
render "new"
end
end
def edit
#movie = Movie.find(params[:id])
end
def update
#movie = Movie.find(params[:id])
if #movie.update_attributes(params[:movie])
redirect_to "/movies"
else
render "edit"
end
end
def destroy
end
private
def movie_params
params.require(:movie).permit(:name, :genre, :year)
end
end
Here's my edit.html.erb
<h1>Now Editing:</h1>
<h3><%= #movie.name %></h3>
<%= form_for #movie.name do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :genre %>
<%= f.text_field :genre %>
<br>
<%= f.label :year %>
<%= f.number_field :year %>
<br>
<%= f.submit "Update" %>
and the routes.rb file:
MovieApp::Application.routes.draw do
get "movies" => "movies#index"
post "movies" => "movies#create"
get "movies/new" => "movies#new"
get "movies/:id" => "movies#show"
get "movies/:id/edit" => "movies#edit"
put "movies/:id" => "movies#update"
end
last, here's the output from running rake routes:
Prefix Verb URI Pattern Controller#Action
movies GET /movies(.:format) movies#index
POST /movies(.:format) movies#create
movies_new GET /movies/new(.:format) movies#new
GET /movies/:id(.:format) movies#show
GET /movies/:id/edit(.:format) movies#edit
PUT /movies/:id(.:format) movies#update
form_for #movie.name should be form_for #movie. I can't tell what's going on, but I suspect this is somehow giving you a <form action="">.
Your error message shows that you are sending a post request to the edit url.
No route matches [POST] "/movies/1/edit"
Whereas in the route you have specified a get request.
get "movies/:id/edit" => "movies#edit"
I believe that is somehow causing the problem and so you could change the request to post.
post "movies/:id/edit" => "movies#edit"
in index file if you are using
button_to 'Edit', edit_movie_path(movie)
change it to
link_to 'Edit', edit_movie_path(movie)
because button send it as POST but the link will send it as GET.

NoMethodError in PostsController#create following the getting started guide

I am a Rails newbie and therefore I am following the getting started guide, available here: http://edgeguides.rubyonrails.org/getting_started.html and here: http://guides.rubyonrails.org/getting_started.html but I can't get the point 5.6 / 5.7 to work.
This is my controller:
class PostsController < ApplicationController
def new
end
def show
#post = Post.find(params[:id])
end
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
and this is my form:
<%= form_for :post, url: posts_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %> <br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
This is the routes.rb
Blog::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resource :posts
end
but when I submit it, I get this error:
NoMethodError in PostsController#create
undefined method post_url' for #<PostsController:0x007f733c415418> with the extract source highlighting the line redirect_to #post.
What am I doing wrong? I have ruby 1.9.3 and rails 4.0.0
In your routes.rb I see you've
resource :posts
I believe, it should be:
resources :posts
You might have missed to add the Post part in your routes. Try running rake routes and see what results you get on this :
rake routes | grep post
if you have mentioned post in your routes then may be you are using the wrong path here.
Did you add the following line to your config/routes.rb?
resources :posts

Having trouble with Rails Routing after form submission

So I'm trying to make a form that would allow users to comment on specific posts. Im currently having trouble after the submit button is clicked. The entry does get put into the database, however it looks like im experiencing some routing problems.
Here is the URL I get redirected to:
"localhost:3000/groups/13/posts/62/comments"
I get the following error after submission:
No route matches {:action=>"show", :controller=>"groups"}
I ran rake routes to find this:
group GET /groups/:id(.:format) groups#show
Here is my comments controller:
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment].merge({:user_id => current_user.id}))
redirect_to :action => :show, :controller => :groups
end
end
Here is my form for the comment:
<%= form_for([post.group, post, post.comments.build]) do |f| %>
  <p>
    <%= f.label :comment %><br />
    <%= f.text_area :body, :rows => 3, :cols => 55 %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>
Does anyone have any ideas what might be wrong? Why is it redirecting to the url "localhost:3000/groups/13/posts/62/comments"
Thanks
I would do:
class CommentsController < ApplicationController
respond_to :html
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment]) do |comment|
comment.user = current_user # user_id shouldn't be an attr_accessible
end
respond_with #comment, location: group_path(#post.group)
end

Resources