【问题标题】:How to Create a Sidebar with Multiple Controller Methods?如何创建具有多个控制器方法的侧边栏?
【发布时间】:2015-02-26 23:00:39
【问题描述】:

我有多个控制器,我想将它们的一些方法放到我的侧边栏中:

habits_controller
goals_controller
valuations_controller
quantifieds_controller
users_controller

如何在不出现未定义方法错误的情况下执行此操作?

我尝试创建一个 sidebar_controller,但我应该在其中包含什么才能使其正常工作?

让我们以习惯为例,然后我可以将我学到的经验应用到我自己的其他控制器上。

摘自_sidebar.html.erb

<div id="sidebarheadingtop" class="panel-heading"><h5><b>Today</b></h5></div>
	<% @habits.each do |habit| %>
	      <td><%= raw habit.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %></td>
  <% end %>
</div>

habits_controller

class HabitsController < ApplicationController
  before_action :set_habit, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @habits = Habit.tagged_with(params[:tag])
    else
      @habits = Habit.all.order("date_started DESC")
      @habits = current_user.habits
    end
  end

  def show
  end

  def new
    @habit = current_user.habits.build
  end

  def edit
  end

  def create
    @habit = current_user.habits.build(habit_params)
    if  @habit.save
        redirect_to @habit, notice: 'Habit was successfully created.'
    else
        @feed_items = []
        render 'pages/home'
    end
  end

  def update
    if @habit.update(habit_params)
      redirect_to @habit, notice: 'Habit was successfully updated.'
    else
      render action: 'edit'
    end
  end

  def destroy
    @habit.destroy
    redirect_to habits_url
  end

  private
    def set_habit
      @habit = Habit.find(params[:id])
    end

    def correct_user
      @habit = current_user.habits.find_by(id: params[:id])
      redirect_to habits_path, notice: "Not authorized to edit this habit" if @habit.nil?
    end

    def habit_params
      params.require(:habit).permit(:missed, :left, :level, :date_started, :trigger, :target, :positive, :negative, :tag_list, :committed => [])
    end
end

sidebar_controller

class SidebarController < ApplicationController
  def index
    @habits = current_user.habits
  end
end

我还是一个初学者,所以任何帮助将不胜感激。谢谢=]

Githubhttps://github.com/RallyWithGalli/ruletoday

【问题讨论】:

    标签: ruby-on-rails ruby methods controller sidebar


    【解决方案1】:

    如果我正确理解您的问题:您希望从各种控制器中获取方法,然后在侧边栏控制器中调用这些方法。

    在我看来,这违反了惯例,听起来有点奇怪。如果你想让你的应用程序restful,你不应该从另一个控制器调用一个控制器的方法。

    如果您希望跨多个控制器混合方法(编写一次方法,然后将该方法混合到一堆控制器中,以便所有这些控制器都有自己的方法副本)然后采用该共享方法并将其放入关注,然后将include 关注到您的控制器中。在您的情况下:include 将该模块添加到您的侧边栏控制器中。

    app/controllers/concerns/shared.rb

    module Shared
        def someMethod
            @someVariableToPassToView = 33
        end
    end
    

    app/controllers/sidebar_controller.rb

    include Shared
    # Your shared controller has now mixed in all the methods from within the 
    # Shared module ( in this case, the someMethod method)
    

    当然不要忘记创建相应的视图来匹配侧边栏的混入方法:someMethod:

    app/views/sidebar/someMethod.html.erb

    <p> Outputting the contents of the variable someVariableToPassToView </p>
    <p><%= @someVariableToPassToView<%></p>
    

    另外,不要忘记更新您的 routes.rb 文件,以便将您混合到侧边栏中的操作,以便 Rails 知道如何访问适当的视图文件:

    get 'sidebar/somemethod'
    

    回答您的问题:当我们想让控制器中的变量可用于视图时,我们将其作为实例变量传递。这就是@ 的用途,使该变量成为实例变量,以便我们可以使用&lt;%= %&gt; 表示法从视图中提取其内容。这就是我在上面作为示例所做的,将数字 33 从它存储的变量中提取出来:@someVariableToPassToView

    我强烈推荐以下内容:

    观看 Kevin Skoglund 的“Ruby On Rails 4 基本培训”。它大约 13 小时长,是我在网上找到的最好的 Rails 介绍。位于此处:http://www.lynda.com/Ruby-Rails-tutorials/Ruby-Rails-4-Essential-Training/139989-2.html

    如果您像我一样,您将观看大约三遍培训结束,然后多次参考培训视频的各个部分。在您适应之后,前往 www.codeschool.com 并观看他们的课程:“Rails 4: Zombie Outlaws”和“Rails 4 Patterns”。

    当然,在深入研究 Rails 之前,您需要确保自己对 ruby​​ 编程语言有扎实的了解。一旦你把它放到上下文中,Rails 会做得更多:Rails 只是 ruby​​ 代码。 Kevin Skoglund 有一个关于 Ruby 的优秀培训视频:http://www.lynda.com/Ruby-tutorials/essential-training/47905-2.html

    祝你好运!

    【讨论】:

    • 非常感谢您的回复!我很惊讶这个问题没有得到更多的解决。我希望你能澄清一点,因为我仍然很困惑。如果您要使用上面的习惯示例,@someVariableToPassToView = 33 是什么意思。我不明白= 33 是什么意思?
    • 更新了我上面的回复。如果我帮助了你,和/或我引导你朝着正确的方向前进,请务必投票和/或在我的回复中打勾。
    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2020-10-26
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多