如何在 Ruby on rails 中访问控制器中的 hidden_​​field [英] How to access hidden_field in controller in Ruby on rails

查看:51
本文介绍了如何在 Ruby on rails 中访问控制器中的 hidden_​​field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何从文件 view/comments/_comment.html.erb 访问隐藏字段值 post_id 并在 中使用它>controllers/dashboards_controller.rb?- 有 2 个控制器 - 仪表板和评论,并使用 gem act_as_commentable_with_threading

现在我得到: ActiveRecord::RecordNotFound in DashboardsController#index 找不到没有 ID 的帖子

<小时>

config/routes.rb

 资源 :comments, :only =>[:创建,:销毁]

controllers/dashboards_controller.rb

class DashboardsController <应用控制器定义索引@post = Post.new@user = current_user@newest_users = User.newest_players@feed_posts = Post.paginate(:page => params[:page], :per_page => 8)@last_clubs = Club.last_clubs@commented_post = Post.find(params[:post_id])# 尝试从 view/comments/_comment.html.erb 访问参数# 评论是另一个控制器...# 用@commented_post 做其他操作@comments = @commented_post.comment_threads.order('created_at desc')@new_comment = Comment.build_from(@commented_post, current_user, '')结尾结尾

view/comments/_comment.html.erb添加评论

意见表的地方

<%= form_for :comment, :remote =>真的做 |f|%><%=f.hidden_​​field 'post_id', post.id %># 需要在 dasboard 控制器中使用这个值<%=f.text_field :body %><%结束%>

view/dashboards/_feed_post.html.erb

    <%if @feed_posts.any?%><% @feed_posts.each 做 |post|%><li><span class="image"><%= image_tag post.image.url(:message) if post.image?%></span><span class="content"><%= post.text_html %></span><span class="tags">标签:<%= post.tag_list %></span><span class="meta">已发布 <%= time_ago_in_words(post.created_at) %>前.|<%= post.user.full_name %></span><%= render 'comments/form' ,:locals =>{:评论=>@new_comment, :post_id =>post.id } %><%= 渲染 'comments/comment', :collection =>@comments, :as =>:comment, :post_id =>post.id %><%结束%><%结束%>

视图/仪表板/索引

<div class="span7"><!--用于创建新帖子的表单--><部分><%= 渲染:模板 =>'帖子/新' %></节><!--dashboard feed_post--><部分><%=渲染:部分=>'仪表板/feed_post' %></节>

解决方案

你正在使用 f.hidden_​​field 所以你会得到

<%=f.hidden_​​field 'post_id', post.id %>

将创建以下 html 引用 hidden_​​field

因此您可以在控制器中按以下方式访问它

params[:comment][:post_id]

所以用下面的代替

@commented_post = Post.find(params[:comment][:post_id])

如果您想在 params[:post_id] 中使用post_id",请使用 hidden_​​field_tag 如下所示

<%= hidden_​​field_tag 'post_id', post.id %>

Question: How to access hidden field value post_id from file view/comments/_comment.html.erb and use it in controllers/dashboards_controller.rb? - there are 2 controllers - dashboard and comments, and using gem act_as_commentable_with_threading

Now I get: ActiveRecord::RecordNotFound in DashboardsController#index Couldn't find Post without an ID


config/routes.rb

  resources :comments, :only => [:create, :destroy]

controllers/dashboards_controller.rb

class DashboardsController < ApplicationController
  def index
    @post = Post.new
    @user = current_user
    @newest_users = User.newest_players
    @feed_posts = Post.paginate(:page => params[:page], :per_page => 8)
    @last_clubs = Club.last_clubs


    @commented_post = Post.find(params[:post_id])
    # trying to access params from view/comments/_comment.html.erb
    # comments is another controller...

   # do other operations with @commented_post   
     @comments = @commented_post.comment_threads.order('created_at desc') 
     @new_comment = Comment.build_from(@commented_post, current_user, '')

  end

end

view/comments/_comment.html.erb Add comment

place for a comment form

<div class="comment-form">
  <%= form_for :comment, :remote => true do |f| %>
  <%=f.hidden_field 'post_id', post.id %>

  # need to use this value in dasboard controller
  <%=f.text_field :body %>

  <% end %>

</div>

view/dashboards/_feed_post.html.erb

<ul class="post-items">


  <%if @feed_posts.any? %>

    <% @feed_posts.each do |post| %>
    <li>
        <span class="image"><%= image_tag post.image.url(:message) if post.image?%></span>
        <span class="content"><%= post.text_html %></span>
        <span class="tags">Tags:<%= post.tag_list %></span>
        <span class="meta">

          Posted <%= time_ago_in_words(post.created_at) %> ago.
      |      <%= post.user.full_name %>
        </span>

      <%= render 'comments/form' ,:locals => { :comment => @new_comment, :post_id => post.id } %>
      <%= render 'comments/comment', :collection => @comments, :as => :comment, :post_id => post.id %>

    </li>
        <% end %>

  <% end %>
</ul>

view/dashboards/index

<div class="row">
  <div class="span7">


    <!--form for creating a new post-->
    <section>
    <%= render :template =>  'posts/new' %>
    </section>


    <!--dashboard feed_post-->
    <section>
    <%= render :partial => 'dashboards/feed_post' %>
    </section>    
  </div>

解决方案

you are using f.hidden_field so you will get

<%=f.hidden_field 'post_id', post.id %>

will create following html ref hidden_field

<input type="hidden" id="comment_post_id" name="comment[post_id]" value="#{comment.post_id}" />

so you can access this as following in your controller

params[:comment][:post_id]

so use following instead

@commented_post = Post.find(params[:comment][:post_id])

if you want 'post_id' in params[:post_id] use hidden_field_tag like following

<%= hidden_field_tag 'post_id', post.id %>

这篇关于如何在 Ruby on rails 中访问控制器中的 hidden_​​field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆