【问题标题】:How to access hidden_field in controller in Ruby on rails如何在 Ruby on rails 中访问控制器中的 hidden_​​field
【发布时间】:2013-08-14 21:06:10
【问题描述】:

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

现在我得到: ActiveRecord::RecordNotFound in DashboardsController#index Couldn't find Post without an ID


config/routes.rb

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

控制器/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

查看/cmets/_comment.html.erb 添加评论

评论表格的地方

<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>

查看/仪表板/_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>

查看/仪表板/索引

<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>

【问题讨论】:

    标签: ruby ruby-on-rails-3 hiddenfield


    【解决方案1】:

    你正在使用f.hidden_field,所以你会得到

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

    将创建以下 html 引用 hidden_field

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

    所以你可以在你的控制器中访问它

    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 %>
    

    【讨论】:

    • @maro10:- 你能把参数发到这里吗?
    • 我认为您在提交表单后调用create方法而不是索引,请发送生成的html以便可以帮助您
    • 感谢您的提示,发现需要改进我的 create 操作,所以继续努力...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    相关资源
    最近更新 更多