"questions", :id=>nil}-6ren"> "questions", :id=>nil}-ruby :2.0.0p0,导轨:3.2.13 我的 rake 路线: questions GET /questions(.:format) questions#index -6ren">
gpt4 book ai didi

ruby-on-rails-3.2 - 没有路由匹配 { :action= >"show", :controller= >"questions", :id=>nil}

转载 作者:行者123 更新时间:2023-12-02 08:41:01 26 4
gpt4 key购买 nike

ruby :2.0.0p0,导轨:3.2.13
我的 rake 路线:

 questions GET    /questions(.:format)          questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy

QuestionsCotroller:类 QuestionsController < ApplicationController

     class QuestionsController < ApplicationController                                                                                                            

def index
@questions = Question.all
end

def show
@question = Question.find(params[:id])
end

def new
@question = Question.new
end

def create
@question = Question.new(params[:question])
# @question.save!
# flash[:notic] = 'Page saved'
# redirect_to :action => 'index'
# rescue ActiveRecord::RecordInvalid
# render :action => 'new'
respond_to do |format|
if @question.save
format.html { redirect_to(@question,
:notice => 'question was successfully created.') }
format.json { render :json => @question,
:status => :created, :location => @question }
else
format.html { render :action => "new" }
format.json { render :json => @question.errors,
:status => :unprocessable_entity }
end
end
end

def edit
@question = Question.find(params[:id])
end

def update
@question = Question.find(params[:id])
# if @question.save
# redirect_to(question_path(@question.id), :notice => t("success update"))
# else
# render :action => "new"
# end
respond_to do |format|
if @question.update_attributes(params[:id])
format.html { redirect_to(@question,
notic: "Question #{@question.title} was successfully updated") }
format.json { head :no_content }
else
format.html { render action: "edit" }
end
end
end
end

edit.html.erb:

<div class="content">                                                                                                                                        
<div class="box">
<%= render 'form' %>
</div>
</div>

_form.html.erb:

<%= simple_form_for @question do |f| %>                                                                                                                      
<fieldset>
<legend><%= @question.new_record? ? t("question.create_topic") : t("questions.edit_topic") %></legend>
<%= f.input :title, :input_html => { :class => "span6" } %>
<%= f.input :content, :as => :text, :input_html => { :class => "span6" } %>
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(params[:question]), :class => "btn btn-danger" %>
</fieldset>
<% end %>

我可以访问 localhost:3000/questions/1,但是当我访问 localhost:3000/questions/1/edit 时,出现错误:没有路由匹配 {:action=>"show", :controller=>"questions", :id=>nil}
这可能是什么问题?如果您需要更多信息,请告诉我。

最佳答案

只需替换这一行,

<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(params[:question]), :class => "btn btn-danger" %>  

通过

<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(@question), :class => "btn btn-danger" %>  

需要将对象传递给路径助手,而不是字符串或整数。

关于ruby-on-rails-3.2 - 没有路由匹配 { :action= >"show", :controller= >"questions", :id=>nil},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16423890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com