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
Related
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
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'
When I try to submit the form its giving me the error
No route matches [POST] "/articles/new"
the files are: new.html.erb
this file which contains the form with a text field and text area:
<%= form_for :article, url: articles_path do |f| %>
here the url: is to match post request which is a create
form's title
<%= f.label :title %><br>
form's text field
<%= f.text_field :title %></p>
form's title
<%= f.label :text %><br>
form's text area
<%= f.text_area :text %></p>
<p>
<%= f.submit %>
</p>
<% end %>
route file is
Rails.application.routes.draw do
resources :article
end
controller is
the controller with its methods new and create
whenever I submit the form its giving the error, even I used the URL: articles_path which for default post request, I used #articles also in the form but it is giving me the same error. I am new to the Rails so I tried many ways but I could not find the solution
class ArticlesController < ApplicationController
def new #new method which is a get request
end
def create #create method which is a post request
end
end
whenever I submit the form its giving the error, even I used the url: articles_path which for default post request. and I kept
def create
end
in the controller
The reason why this occurs for many people using this tutorial is that they don't reload the form after changing the url option in the form_for helper.
Be sure to reload a fresh copy of the form before trying to submit it (you need the form to have the most recent form submission url).
change:
resources :article
to:
resources :articles #plural
that way it will map to:
articles_path POST /articles(.:format) articles#create
I changed app/views/articles/new.html.erb from:
<%= form_for :article do |f| %>
to:
<%= form_for :article, url: articles_path do |f| %>
You can find the answer on the official guideline.
Because this route goes to the very page that you're on right at the moment, and that route should only be used to display the form for a new article.
Edit the form_with line inside app/views/articles/new.html.erb to look like this:
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
Your actions/methods in the controller do nothing. It should be something like:
class ArticlesController < ApplicationController
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit()# here go you parameters for an article
end
end
In the view:
<%= form_for #article do |f| %>
RedZagogulins answer is right - that is the code you need to use in your articles controller
--
Routes
The clue to your problem is here:
No route matches [POST] "/articles/new"
Typically, when using the correct routing structure:
#config/routes.rb
resources :articles #-> needs to be controller name
You'll find that the new action is GET, not POST. This leads me to believe your system is set up incorrectly (it's trying to send the form data to articles/new, when it should just send to [POST] articles
If you follow the steps outlined by RedZagogulin, it should work for you
I was continually running into this problem and it came down to the fact that I had the following code in my navbar:
<%= link_to "Blog", controller: 'articles' %>
I switched to this and it all started working:
<%= link_to "Blog", articles_path %>
I'm still new to this so the different ways of doing things sometimes gets confusing.
In your case,
at first change the
#/config/routes.rb
resources :article
to
#/config/routes.rb
resources :articles #plural
# Changing this in Plural naming if your file name
# in #/app/view
# is plural
# (ex: /app/view/articles)
#
# and controller and it's class is also in plural naming
# (ex:
# in #/app/controllers/articles_controller.rb file
# class ArticlesController < ApplicationController
#
# end)
In some cases, we need to add a Post routes to make a Post Request of the form. Just add the lines in routes file:
#/config/routes.rb
Rails.application.routes.draw do
resources :articles #plural
post '/articles/new' => 'articles#create'
end
In order to get this working, I needed to combine instructions for two answers. First, I added a specific route in routes.rb to handle POST requests to '/articles/new':
Rails.application.routes.draw do
root "articles#index"
resources :articles
post '/articles/new' => 'articles#create'
end
And then I had to change the first line of the form to this <%= form_with model: #aricle do |form| %>:
<h1>New Article</h1>
<%= form_with model: #aricle do |form| %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
I am newbie to ruby on rails. I am getting this error. I have followed this tutorial http://www.codelearn.org/ruby-on-rails-tutorial/forms-form_tag-params-attr_accessible-model-validation
for the form posting. But when I click on submit i was getting an error
this is my
controller
class CompanyratingController < ApplicationController
def index
#companies = Companyrating.all
end
def add
#companies.create(:companies => params[:name, :place, :rate, :rank])
#redirect_to :action => 'index'
#companies = Companyrating.new(params[:name])
if #companies.save
flash[:success] = "Welcome to My Space!"
redirect_to root_url
end
end
end
this is modal
class Companyrating < ActiveRecord::Base
attr_accessible :name, :place, :rate, :rank
end
this is my routes file
get "companyrating/index"
match "companyrating/add" => "todos#add", :via => :post
my index file
<title>Shared Todo App </title>
<h1>Shared Todo App</h1>
<p>All your todos here</p>
<ul><li> <% #companies.each do |t| %>
<li> <%= t.companies_name %> </li>
<li> <%= t.companies_place %> </li>
<li> <%= t.companies_rate%> </li>
<li> <%= t.companies_rank %> </li>
<% end %>
</li></ul>
<%= form_for("#companies/add", :method=>"post") do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :place %>
<%= f.text_field :place %>
<%= f.label :rate %>
<%= f.text_field :rate %>
<%= f.label :rank %>
<%= f.text_field :rank %>
<%= f.label :user_id%>
<%= f.text_field :user_id %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
This was my error: No route matches [POST] "/companyrating/index"
can anyone help me in solving this error
in controller:
def new
#companyrating = Companyrating.new
end
def create
#companies = Companyrating.new(params[:companyrating])
if #companies.save
flash[:success] = "Welcome to My Space!"
redirect_to root_url
else
flash[:error] = "Can't create companyrating."
render 'new'
end
end
in routes.rb:
resources :companyrating
in form:
<%= form_for(#companyrating, method: :post) do |f| %>
The problem you're having is two-fold - with your routes and controller actions
I know Monk_Code gave an answer, but since you're new, I'll explain how it all works for you, with the goal of helping you understand the system a bit better:
Routes
Your first issue is to do with your routes
Rails has done an amazing job of creating great routing structures, and the core of this is the idea of resourceful routes
By using the following code, your Rails app creates a series of 7 routes which Rails uses to send "default" traffic to your controller actions:
#config/routes.rb
resources :companyratings
This routing structure creates the following routes in your app:
new [GET]
index [GET]
create [POST]
edit [GET]
update [POST]
destroy [DELETE]
show [GET]
All of these routes take directed traffic, and routes it to the relevant controller action. This means that if you send a user to /companyratings/, it's going to load the index action, likewise /companyratings/15 will show the show action in the controller
On top of this, you also need to know what the HTTP "verb" does. This is a key principle which Rails uses to route your traffic accordingly. The "verb" in your request is the type of request that's sent, and is dependent on the browser
As you can see from the list above, every route has an associated HTTP verb. The reason why this is important is because if you send a [GET] request to a route which only works with the [DELETE] verb, you're going to get a problem, hence why you're seeing errors when you send a [POST] request to your index action
You should read the Rails tutorial on this, as you can either fix the issue by sending the correct HTTP verb (using :method => :get), or specify the [POST] verb in your index action route
Controller Actions
Controller actions are the function defined in your controller, which are loaded when you run a particular request in Rails
The default index actions are listed above, but you can also have any others you like, as long as you provide the correct routes. Your problem is that you've just used the add action, where you should have used the create action:
class CompanyratingController < ApplicationController
def index
#companies = Companyrating.all
end
def new
#companyrating = Companyrating.new
end
def create
#companies = Companyrating.new(new_company_rating)
if #companies.save
flash[:success] = "Welcome to My Space!"
redirect_to root_url
end
end
private
def new_company_rating
params.require(:company_rating).permit(:variables, :listed, :here)
end
end
While learning Rails 4 I stuck while trying to add categories to posts of my simple blog. I have generated model, ran the migration and added a controller. No matter what I do now when trying to create a category, I keep running into same mistake: no route matches [POST], which is weird, as I seem to have all the code in place. Please help!
categories controller
class CategoriesController < ApplicationController
def index
#categories = Category.all
end
def new
#category = Category.new
end
def create
#category = Category.new(category_params)
#category.save
redirect_to new_category_path, alert: "Category created!"
end
def show
#category = Category.find(params[:id])
end
def destroy
#category = Category.find(params[:id])
#category.destroy
redirect_to categories_path
end
private
def category_params
params.require(:category).permit(:name)
end
end
routes.rb
Blog::Application.routes.draw do
get 'tags/:tag', to: 'posts#index', as: :tag
resources :categories
resources :posts do
resources :comments
end
root 'welcome#index'
end
category.rb
class Category < ActiveRecord::Base
validates :name, presence: true
has_many :posts
end
new.html.erb
<%= form_for :category do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
/categories/new
No route matches [POST] "/categories/new"
You should have in your view
<%= form_for #category do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
#category object is used by form_for method to figure out form url.
If you pass only Symbol to form_for method, without specifying url explicitly, form will be generated with url being the current url.
Although the #category works, if you read a bit further you will see that they will explain why the your code sends No route matches [POST] "/categories/new".
The guide actually explains that you need to specify the url: posts_path for the form to use the right route.
There's one problem with this form though. If you inspect the HTML
that is generated, by viewing the source of the page, you will see
that the action attribute for the form is pointing at /posts/new. This
is a problem because this route goes to the very page that you're on
right at the moment, and that route should only be used to display the
form for a new post.
The form needs to use a different URL in order to go somewhere else.
This can be done quite simply with the :url option of form_for.
Typically in Rails, the action that is used for new form submissions
like this is called "create", and so the form should be pointed to
that action.
Edit the form_for line inside app/views/posts/new.html.erb to look
like this:
<%= form_for :post, url: posts_path do |f| %>
In this example, the
posts_path helper is passed to the :url option. What Rails will do
with this is that it will point the form to the create action of the
current controller, the PostsController, and will send a POST request
to that route.