【发布时间】:2015-11-10 15:19:33
【问题描述】:
由于某种原因,编辑操作不会渲染我收到此错误,并且正在使用显示操作而不是编辑,但相同的表单适用于渲染:新操作
- 不要关注params[:preview],我说的是最后一个
render :edit
ActionController::UrlGenerationError in Admin::Blog::Posts#update
No route matches {:action=>"show", :controller=>"admin/blog/posts", :id=>""} missing required keys: [:id]
def edit
@post = Post.find_by_permalink(params[:id])
end
def update
@post = Post.find_by_permalink(params[:id])
if params[:publish]
@post.publish
elsif params[:draft]
@post.draft
end
if params[:preview]
if @post.published?
@post.draft
end
if @post.update(blog_post_params)
flash[:success] = "some text "
redirect_to blog_post_url(@post)
else
render :edit
end
end
if @post.update(blog_post_params)
flash[:success] = "Post was successfully updated."
redirect_to edit_admin_blog_post_url(@post)
else
render :edit
end
end
表格
<%= form_for [:admin,:blog, @post] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="large-12 columns">
<div class="field panel">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field panel">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="actions panel text-right">
<% if @post.published? %>
<%= f.submit "Save Changes",name: "publish", class: "tiny button radius success" %>
<% else %>
<%= f.submit "Publish",name: "publish", class: "tiny button radius success" %>
<% end %>
<%= f.submit 'Mark as Draft', name: "draft", class: "tiny button radius " %>
<% if @post.created_at %>
<%= f.submit 'Preview', name: "preview", class: "tiny button radius warning" %>
<% end %>
<%= link_to 'Back', admin_blog_posts_path, class: "tiny button radius secondary" %>
</div>
<div class="field panel">
<%= f.label :body %><br>
<%= f.cktext_area :body, :height => '800px', :id => 'sometext' %>
</div>
</div>
<% end %>
相关路线
namespace :admin do
namespace :blog do
get '', to: 'welcome#index', as: '/'
resources :posts
end
end
发布模型
class Post < ActiveRecord::Base
has_many :tags
has_many :comments
before_validation :generate_permalink
validates :permalink, uniqueness: true
validates :title, presence: true
validates :description, presence: true
def generate_permalink
self.permalink = title.parameterize
end
def to_param
permalink
end
end
【问题讨论】:
-
您如何进行编辑操作?你点击的链接是什么?
-
添加了表单。点击保存更改按钮@japed
-
能把你的config/routes.rb文件的相关内容贴出来吗?
-
不应该是
redirect_to admin_blog_post_url(@post)吗? -
@Undo 我添加了我的路线谢谢
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4