【问题标题】:Ruby on Rails won't render edit page without idRuby on Rails 不会呈现没有 id 的编辑页面
【发布时间】: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


【解决方案1】:

我想我知道你为什么会收到这个错误。

edit 操作中,您使用Post.find_by_permalink(params[:id]) 查找您的帖子,如果没有找到,则返回nil。而且由于您可以更改title 属性,因此您的永久链接已更新(我猜),并且找不到您的帖子。控制器仍然渲染动作,使用nil@post,并且无法生成表单的url。

尝试改用Post.find_by_permalink!(params[:id]),您将得到 404。

我实际上建议您在管理区域中使用常规的find,因为永久链接可能会更改。

【讨论】:

  • 你是对的。这是根本原因,谢谢!但是我对所有东西都使用相同的模型,有解决方法吗?
  • 您可以尝试在验证后移动设置永久链接的回调,这样只有在对模型的更改有效时才会更改永久链接。
  • 我看不出这对我的情况有何影响,因为问题在于我使用永久链接的方式,有这个函数 to_param 从模型中自动调用。我在上面添加了我的模型以供参考。
  • 我说的不是to_param,我是说你应该把这个:before_validation :generate_permalink改成这个:after_validation :generate_permalink。但当然,您需要验证标题的唯一性。
  • 然后你可以只生成url,避免to_param这样的方法:admin_post_url(@post.id),并使用常规的find方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
相关资源
最近更新 更多