RoR:我怎样才能让我的微博显示出来? [英] RoR: How can I get my microposts to show up?

查看:44
本文介绍了RoR:我怎样才能让我的微博显示出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是用户应该显示的显示视图...

<div id="购买"><%=render 'shared/micropost_form_purchase' %>

<div id="销售额"><%=render 'shared/micropost_form_sale' %>

</节><%=@sales%><%# 这个只是看有没有输出.它没有:(%><div id="采购清单"><ol class="微博"><%= 渲染@purchases 除非@purchases.nil?%></ol>

<div id="销售清单"><ol class="微博"><%= 渲染@sales 除非@sales.nil?%></ol>

所以表格(部分)加载得很好,但是当我发帖时,在任何一个中,采购清单和销售清单都没有出现.我检查了数据库,它们正在与列中指示种类(销售或购买)的条目一起创建.

表格如下:

<%= form_for (@micropost) do |f|%><div class="字段无缩进"><%= f.text_area :content, placeholder: 你还有什么想买的?"%><%= hidden_​​field_tag 'micropost[kind]', "购买" %>

<%= f.submit "Post", class: "btn btn-large btn-primary" %><%结束%>

<%= form_for (@micropost) do |f|%><div class="字段无缩进"><%= f.text_area :content, placeholder: 你还有什么想买的?"%><%= hidden_​​field_tag 'micropost[kind]', "sale" %>

<%= f.submit "Post", class: "btn btn-large btn-primary" %><%结束%>

此外,这里是 users_controller.rb 的显示部分

 def show@user = User.find(params[:id])@micropost=Micropost.new@microposts = @user.microposts.paginate(page: params[:page])结尾

这里是 microposts_controller.rb 的展示部分

def 显示@micropost = Micropost.find(params[:id])@microposts = Micropost.where(:user_id => @user.id)@purchases= @microposts.collect{ |m|m if m.kind == "purchase"}.compact@sales = @microposts.collect{ |m|m if m.kind == "sale"}.compact结尾

此外,在这篇文章(http://stackoverflow.com/questions/12505845/ruby-error-wrong-number-of-arguments-0-for-1#12505865)的帮助下,变量@microposts,@purchase 和 @sales 都在控制台中正确输出.

谁能帮帮我?

按照控制台中给出的答案的建议使用范围(它正确输出所有内容,但它们仍然没有显示在视图中.这是否意味着我的用户显示页面的语法有问题?

这里是 view/microposts/_micropost.html.erb 代码

  • <span class="content"><%= micropost.content %></span><span class="timestamp">已发布 <%= time_ago_in_words(micropost.created_at) %>前.</span><% if current_user?(micropost.user) %><%= link_to "delete", micropost, 方法: :delete,确认:你确定?",标题:micropost.content %><%结束%>
  • 解决方案

    我在没有看到更多代码的情况下做了一些假设,但看起来你可以写下你所展示的有点不同.我假设您的数据库正在迁移并具有所需的列,例如 Micropost#kind、Micropost#user_id 等.您可以使用范围来更有表现力地细化微博集合.阅读可能会有所帮助关于 ActiveRecord 范围:http://guides.rubyonrails.org/active_record_querying.html#scopes.

    class Micropost  "purchase")范围:销售,其中(:种类=>销售")#你的代码结尾

    我还假设您的用户有很多微博:

    class User 

    对于您的表单,我建议将您的隐藏字段附加到表单对象 (f.hidden_​​field) 以便您不必将名称指定为micropost[kind]".

    <%= form_for(@micropost) do |f|%><div class="字段无缩进"><%= f.text_area :content, placeholder: 你还有什么想买的?"%><%= f.hidden_​​field :kind, :value =>销售"%>

    <%= f.submit "Post", class: "btn btn-large btn-primary" %><%结束%>

    在 MicropostsController#show 中,您可以使用新范围:

    def 显示@micropost = Micropost.find(params[:id])@microposts = @user.microposts@purchases = @microposts.purchases@sales = @microposts.sales结尾

    您还应该确认您的 MicropostsController#create 操作实际上正在添加给发送表单的用户的微博(我假设是当前用户方法).

    def 创建@micropost = current_user.microposts.create(params[:micropost])#雅达结尾

    您还可以在创建购买或销售微博后在 rails 控制台上确认预期结果:

    Micropost.purchases微邮销售

    同样,如果没有看到更多代码库,我可能会遗漏一些东西.

    Here is the users show view where they are supposed to show up. ..

    <section>
      <div id= "purchases">
        <%= render 'shared/micropost_form_purchase' %>
      </div>
      <div id="sales">
        <%= render 'shared/micropost_form_sale' %>
      </div>
    </section>
    
    <%= @sales %>  <%# This is just to see if it outputs anything. It doesn't :( %>
    <div id="purchases list">
      <ol class="microposts">
        <%= render @purchases unless @purchases.nil? %>
      </ol>
    </div>
    
    <div id="sales list">
      <ol class="microposts">
        <%= render @sales unless @sales.nil? %>
      </ol>
    </div>
    

    so the forms (partials) are loading fine, but then when I make a post, in either one, neither the purchases list nor the sales list shows up. I checked the database and they are being created along with an entry in the column indicating kind (either sale or purchase).

    Here are the forms:

    <%= form_for (@micropost) do |f| %>
    
      <div class="field no-indent">
        <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
        <%= hidden_field_tag 'micropost[kind]', "purchase" %>
      </div>
      <%= f.submit "Post", class: "btn btn-large btn-primary" %>
    <% end %>
    

    and

    <%= form_for (@micropost) do |f| %>
    
      <div class="field no-indent">
        <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
        <%= hidden_field_tag 'micropost[kind]', "sale" %>
      </div>
      <%= f.submit "Post", class: "btn btn-large btn-primary" %>
    <% end %>
    

    also, here is the show part of the users_controller.rb

     def show
        @user = User.find(params[:id])
        @micropost=Micropost.new
        @microposts = @user.microposts.paginate(page: params[:page])
      end
    

    and here is the show part of the microposts_controller.rb

    def show
    
        @micropost = Micropost.find(params[:id])
        @microposts = Micropost.where(:user_id => @user.id)
        @purchases= @microposts.collect{ |m| m if m.kind == "purchase"}.compact
        @sales = @microposts.collect{ |m| m if m.kind == "sale"}.compact
    
      end
    

    additionally, with the help of this post (http://stackoverflow.com/questions/12505845/ruby-error-wrong-number-of-arguments-0-for-1#12505865) the variables @microposts, @purchases, and @sales are all outputting correctly in the console.

    can anyone help me out?

    edit: using scopes as suggested by the answer given works in the console (it outputs everything correctly, but they still don't show up in the view. Does this mean it is something wrong with my syntax for the users show page?

    edit 2:

    Here is the view/microposts/_micropost.html.erb code

    <li>
      <span class="content"><%= micropost.content %></span>
      <span class="timestamp">
        Posted <%= time_ago_in_words(micropost.created_at) %> ago.
      </span>
      <% if current_user?(micropost.user) %>
        <%= link_to "delete", micropost, method:  :delete,
                                         confirm: "You sure?",
                                         title:   micropost.content %>
      <% end %>
    </li>
    

    解决方案

    I'm making some assumptions without seeing more of your code, but it looks like you could write what you've shown a little differently. I'm assuming your databases are migrating and have the required columns, e.g., Micropost#kind, Micropost#user_id, etc. You can use scopes to refine a collection of microposts more expressively. It might be helpful to read up about ActiveRecord scopes: http://guides.rubyonrails.org/active_record_querying.html#scopes.

    class Micropost < ActiveRecord::Base
      belongs_to :user
    
      scope :purchases, where(:kind => "purchase")
      scope :sales, where(:kind => "sale")
    
      # your code
    end
    

    I'm also assuming your user has many microposts:

    class User < ActiveRecord::Base
      has_many :microposts
    
      # your code
    end
    

    For your forms, I'd suggest attaching your hidden field to the form object (f.hidden_field) so you don't have to specify the name as 'micropost[kind]'.

    <%= form_for(@micropost) do |f| %>
      <div class="field no-indent">
        <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
        <%= f.hidden_field :kind, :value => "sale" %>
      </div>
      <%= f.submit "Post", class: "btn btn-large btn-primary" %>
    <% end %>
    

    In MicropostsController#show, you can use your new scopes:

    def show
      @micropost = Micropost.find(params[:id])
      @microposts = @user.microposts
      @purchases = @microposts.purchases
      @sales = @microposts.sales
    end
    

    You should also confirm that your MicropostsController#create action is actually adding the microposts to the user sending the form (I'm assuming a current user method).

    def create
      @micropost = current_user.microposts.create(params[:micropost])
      # yada
    end
    

    You can also confirm expected results on rails console after creating purchases or sales micropost with:

    Micropost.purchases
    Micropost.sales
    

    Again, I could be missing something without seeing more of the code base.

    这篇关于RoR:我怎样才能让我的微博显示出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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