ruby-on-rails - 为什么将参数传递到 link_to 不起作用?

标签 ruby-on-rails ruby ruby-on-rails-4 hyperlink

在尝试了最初评论中指出的几件事后仍然无法正常工作 - 我在下面进行了修改以反射(reflect)这些努力/澄清一些事情。

问题摘要:我无法创建新帖子(答案),其中该帖子的parent_id 等于相关问题的帖子的id。用户在 posts#show 页面上启动问题帖子,然后单击“回答”。我本以为下面的代码会将问题帖子的 id 传递给答案帖子的parent_id。但事实并非如此。它返回parent_id = nil

我有一个帖子模型。帖子的 post_type_id 为 1(问题)或 2(答案)。

post_type_id = 2 的所有帖子都有一个parent_id,该parent_id 指向正在回答的问题的ID。

在帖子#show 我有:

<% case post_type %> 
  <% when 1 %>
<%= link_to 'Answer', new_post_path(:parent_id => @post.id) %>
  <% else %>
<% end %>

当我点击它时,它会将我带到 new_post 页面,网址为: //localhost:3000/posts/new?parent_id=6

(本例中的问题 id = 6,所以我认为这看起来是正确的。)

在我的帖子 Controller 中,我有:

  def new
    @post = Post.new
  end

  def show
  end

  def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id
    @post.parent_id = params[:parent_id]

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
    end

  def post_params
    params.require(:post).permit(:user_id, :post_type_id, :title, :content, :accepted_answer_id, :parent_id, :score, :answer_count, :favorite_count)
  end

我的路线是:

          posts GET    /posts(.:format)                   posts#index
                POST   /posts(.:format)                   posts#create
       new_post GET    /posts/new(.:format)               posts#new
      edit_post GET    /posts/:id/edit(.:format)          posts#edit
           post GET    /posts/:id(.:format)               posts#show
                PATCH  /posts/:id(.:format)               posts#update
                PUT    /posts/:id(.:format)               posts#update
                DELETE /posts/:id(.:format)               posts#destroy

在 new.html.erb 中我有:

<h3>New Post</h3>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>

而 _form.html.erb 是:

<%= simple_form_for(@post) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%- f.association :user %>
<%= f.input :post_type_id, collection: post_types, label: 'What do you want to do?' %>
<%= f.input :title %>
<%= f.input :content %>
<%- f.input :accepted_answer_id %>
<%- f.input :parent_id %>
<%- f.input :score %>
<%- f.input :answer_count %>
<%- f.input :favorite_count %>
</div>

<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

我非常感谢任何有关如何使其正常工作的建议。

谢谢!

最佳答案

好的,您的用户点击答案,然后将问题 ID 作为 ROOT 参数转发 ( params[:parent_id] )

现在,您想告诉您的新操作,您正在构建的帖子是问题的答案,因此您应该在此处使用参数

controllers/posts_controller.rb

def new
  @post = Post.new
  @post.parent_id = params[:parent_id] if params[:parent_id]

然后在您的表单中,您可以添加一个隐藏字段(用户不关心 ID)。相信如果场.parent_id没有设置,会留下空白值,没关系

views/posts/_form.html.erb

<%= f.hidden_field :parent_id %>

然后,当您发布新答案时,:parent_id 将被发送,您无需担心

controllers/posts_controller.rb

def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id
    # No need for the below line, :parent_id is already in post_params
    # @post.parent_id = params[:parent_id]

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  def post_params
    params.require(:post).permit(..., :parent_id, ...)
  end

编辑:回答评论中的问题

为什么@post.parent_id = params[:parent_id]属于#new

通过这一行,您实际上设置了为 #new 操作准备的“模板 @post 对象”的 :parent_id 内存变量。现在,当您渲染 View 时,默认情况下每个表单助手都会加载内存中的值(如果存在)。当你在做f.input :content时,form_builder 将生成 HTML 标签,但也会在内存中查找 @post.content 。由于模型是空白生成的,因此没有值,因此它将显示一个空字段。因此,当您调用f.hidden_field :parent_id时,它实际上会找到变量(感谢我们刚刚添加的代码)并使用它。

<%- 而不是 <%=

我个人从不使用<%- ,我不得不用谷歌搜索,发现this question<%-只会避免换行,所以它也可以工作

为什么是隐藏字段?

这只是用户体验的问题。隐藏字段将写入 HTML,但会对用户隐藏。用户根本不在乎你的 :parent_id 。您正在使用 ActiveRecord,因此 ID 看起来非常好,但如果您尝试其他数据库系统,您的 ID 可能看起来像 0bef0600008de 。而且您的用户肯定不关心他正在添加问题的答案 0bef0600008de .

但是 h 可能对此感兴趣:

_form.html.erb

<% if @post.parent_id %> <!-- if variable parent_id exists <=> we are writing an answer -->
  <h2>You are writing an answer for the question <%= Post.find(@post.parent_id).title %></h2>
<% end %>

关于ruby-on-rails - 为什么将参数传递到 link_to 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006005/

相关文章:

ruby-on-rails - 如何验证一个字符串是一年

ruby-on-rails - ruby 矿 'Unable to attach test reporter to test framework'

ruby-on-rails - 是在 mixin 模块中声明 Active Record Associations 的有效方法吗?

ruby-on-rails - 你如何在 Rails 4 中实现虚拟 link_to

ruby-on-rails - 覆盖 rails #destroy 参数错误

ruby-on-rails - API/JSON : Can't verify CSRF token authenticity

ruby - 一次从文件中读取多行

php - 从脚本语言中调用 shell 命令会降低性能吗?

javascript - 无法读取未定义的属性 'Api' 来加载响应式数据表.js

ruby-on-rails - CanCan 跳过某些操作的授权