为什么rails应该在编辑现有帖子时创建新帖子?
我的form_for中有method: :post
,这可能是为什么它创建帖子而不是使用get方法编辑它,但是如果我尝试采用该方法,整个表单将停止工作。
形式:
<div class="signin">
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="well boxShadow">
<div class="media">
<div class="media-body">
<h4 class="media-heading red">New Question</h4>
<hr />
<%= form_for(@question, url: questions_path, html: { method: :post, :class => 'form-horizontal' }) do |f| %>
<% if @question.errors.any? %>
<div id="error_explanation">
<h4><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h4>
<ul>
<% @question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="col-sm-12">
<%= f.text_field :title, :class => "form-control outline", :placeholder => "Enter your question" %>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<%= f.text_area :description, :class => "form-control outline", rows: 15 %>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<%= f.submit "Ask", :class => "btn btn-danger btn-md btn-block" %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
</div>
路线:
Rails.application.routes.draw do
resources :questions
devise_for :users
root 'home#index'
get '/users/:id' => 'home#profile'
resources :users
resources :questions do
resources :answers
end
resources :answers
end
显示视图:
<div class="content">
<div class="container">
<h3 class="media-heading ques"><%= @question.title %></h3>
<hr />
<div class="row">
<div class="col-md-8">
<div class="well">
<div class="media">
<div class="media-body">
<br/>
<p><%= @question.description %></p>
</div>
<div class="pull-right">
<% if user_signed_in? %>
<% if @question.user_id == current_user.id %>
<%= link_to 'edit', edit_question_path(@question), :class => "text-muted links" %>
<%= link_to 'delete', question_path(@question), :method => :delete, :class => "text-muted links" %>
<% end %>
<% end %>
</div>
</div>
</div>
<!-- About the auther -->
<div class="well">
<div class="media">
<a class="pull-left">
<% if @question.user.avatar.blank? %>
<img src="http://www.adtechnology.co.uk/images/UGM-default-user.png" style="width: 75px;">
<% elsif @question.user.avatar %>
<%= image_tag @question.user.avatar, :style => "width:75px;" %>
<% end %>
</a>
<div class="media-body">
<p>About <%= link_to @question.user.username, @question.user, :class => " bg" %></p>
</div>
<p class="text-muted small">Apparently this user doesn't like to share his information.</p>
</div>
</div>
<!-- Answers -->
<div class="well">
<div class="media">
<h4 class="media-heading red">Answers</h4>
<hr>
<%= render :partial => @question.answers %>
</div>
</div>
<!-- Answer form -->
<div class="well boxShadow">
<div class="media">
<div class="media-body">
<h4 class="media-heading red">Answer</h4>
<hr />
<%= form_for [@question, Answer.new], :class => 'form-horizontal' do |f| %>
<div class="form-group">
<div class="col-sm-12">
<%= f.text_area :body, :class => "form-control outline", rows: 15 %>
</div>
</div>
<br/><br/>
<div class="form-group">
<div class="col-sm-12">
<%= f.submit "Answer", :class => "btn btn-danger btn-md btn-block mar20" %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="blogC">
<br>
<p><span style="padding-right: 10px;" ><i class="fa fa-calendar"></i> asked <%= time_ago_in_words(@question.created_at) %> ago </span></p>
</div>
<br/>
<div class="blogC">
<h4 class="red">Recent Blogs</h4>
<hr />
<p><a href="#">Test blog 123 123</a></p>
<p><a href="#">Test blog 123 123</a></p>
<p><a href="#">Test blog 123 123</a></p>
<p><a href="#">Test blog 123 123</a></p>
<p><a href="#">Test blog 123 123</a></p>
<p><a href="#">Test blog 123 123</a></p>
<p><a href="#">Test blog 123 123</a></p>
</div>
<br/>
<div class="blogC">
<h4 class="red">Sponsors</h4>
<hr />
<img src="http://webneel.com/daily/sites/default/files/images/daily/03-2013/4-animal-rights-sweden-boxer-animal-ad.jpg" class="img-responsive">
</div>
</div>
</div>
</div>
</div>
问题控制员:
class QuestionsController < ApplicationController
before_action :set_question, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@questions = Question.all
respond_with(@questions)
end
def show
@questions = Question.all.order("created_at desc")
respond_with(@question)
end
def new
if user_signed_in?
@question = current_user.questions.build
respond_with(@question)
else
redirect_to new_user_session_path
end
end
def edit
end
def create
@question = current_user.questions.build(question_params)
if @question.save
redirect_to question_path(@question)
else
render 'new'
end
end
def update
@question.update(question_params)
respond_with(@question)
end
def destroy
@question.destroy
respond_with(@question)
end
private
def set_question
@question = Question.find(params[:id])
end
def question_params
params.require(:question).permit(:title, :description)
end
end
完整追踪:
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
服务器日志:
Started PUT "/questions" for 127.0.0.1 at 2016-02-26 06:23:52 -0800
ActionController::RoutingError (No route matches [PUT] "/questions"):
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `c
all'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `ca
ll'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tag
ged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware
.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `servi
ce'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in
start_thread'
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/routes/_route.html.erb (4.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within
答案 0 :(得分:1)
您需要使用PUT
更新 记录。以下应该有效
<%= form_for(@question, url: questions_path, html: { method: :put, :class => 'form-horizontal' }) do |f| %>
答案 1 :(得分:1)
好的,所以我先修复了路线中重复的问题资源:
更新路线
Rails.application.routes.draw do
devise_for :users
root 'home#index'
get '/users/:id' => 'home#profile'
resources :users
resources :questions do
resources :answers
end
resources :answers
end
然后我为编辑视图创建了一个新的部分并更新了这一行:
发件人:强>
<%= form_for(@question, url: questions_path, html: { method: :post, :class => 'form-horizontal' }) do |f| %>
要强>
<%= form_for(@question, html: { method: :put, :class => 'form-horizontal' }) do |f| %>