Tag: 模型 视图 控制器

什么是Ruby on Rails中的MVC?

有人可以用Ruby on Rails的方式向我解释MVC。 我对理解MVC中的模型特别感兴趣(无法理解模型)。

初学者使用Rails 3.1和“静态”页面

我刚刚开始部署Rails网站。 事实上,我在2天前开始在Rails上编程。 我已经创建了一个新项目,添加了一些gem等等。一切都运行正常,我对所有工作的基本知识都有所了解。 问题是我想要创建的是一个简单的网站,其中包含一些部分(例如,新闻,联系人,关于,产品……)。 所有这些内容都是静态的。 但我遇到了一个问题。 我真的不知道该怎么做才能创造它们。 我想要的是,例如mypage.com/about等,甚至mypage.com/page/products 。 我考虑过创建一个Controller,然后为每个页面创建一个动作……之后,我想出了其他解决方案:scaffolding。 创建名为page的资源,具有title等… 我真的是这个主题的初学者,我想听听你有用的声音。 谢谢!

获取在控制器/视图中工作的方法在模型中运行,Ruby on Rails

我正在尝试根据用户的位置向位置列下的用户模型添加字符串。 我已经完成了所有设置,我知道@ city + @ state的值被添加到正确模型中的相应列中。 问题是,似乎request.location.city和request.location.state在控制器和视图中正常运行,但在模型中却没有。 def add_location @city = request.location.city @state = request.location.state @location = @city+@state self.location = @location end 创建用户时,不会创建任何字符串,例如“losangelescalifornia”。 当我定义@city =“bob”和@state =“cat”时,所有创建的用户都在适当的位置有“bobcat”。 我知道除了这些基于地理定位的方法之外,一切都在运行。 所以我的问题是,如何在模型中运行这些方法(请纠正我,如果它不是它们),即request.location.city和request.location.state? 提前谢谢了 :)

无法在视图Rails 4中显示我的类别和子类别

我的网站有分类和子类别,这里是我在我的种子.rb,我创建了一个名为“video和动画”的主要类别,和4个子类别,比我将子类别分配到主类别。 @category = Category.create!(name: “Video and animation”) [“Intro”, “Animation & 3D”, “Editing and Post Production”, “Other”].each do |name| @subcategory = Subcategory.create!(name: name, category_id: @category.id) end 它运行良好,在我的rails控制台中,我看到一切正常,添加产品的category_id更改为数字(整数应该如此)。 问题:如何在我的视图中显示该类别,因此当有人点击它时,他会获得该类别的所有产品。 我如何按照相同的原则显示所有子类别。 以下是我试图提出意见的内容 奇怪的是,当我把我的产品控制器放进去时 def index @category_id = Category.find_by(name: params[:category]) @gigs = Gig.where(category_id: @category_id).order(“created_at DESC”) @subcategory_id = Subcategory.find_by(name: params[:subcategory]) @gigs = Gig.where(subcategory_id: @subcategory_id).order(“created_at DESC”) end 它显示了我想要的所需子类别,但主要类别仍为空。 如果我把它放入我的控制器 def index […]

如何在多个模型的会话中存储数据?

在@blnc的帮助下,我们能够在他的会话中存储用户的目标。 然后,一旦他注册,将该目标保存到他的新帐户并从会话中删除它。 作为注册过程的另一个步骤,我希望用户在注册之前也能养成习惯。 goals_controller def create if current_user == nil # If there is no user, store the goal values to the session session[:goal_name] = goal_params[:name] session[:goal_deadline] = goal_params[:deadline] redirect_to signup_url else @goal = current_user.goals.build(goal_params) if @goal.save track_activity @goal redirect_to @goal, notice: ‘Goal was successfully created’ else flash.now[:danger] = ‘Required Field: “Enter Goal”‘ render ‘new’ […]

如何添加多态注释到Feed?

我正在尝试允许用户直接在他们的活动源上添加和查看评论,而不必去显示页面,这是我在这个railscast剧集中所做的: http ://railscasts.com/episodes/ 154-多态关联 – 修订 。 我从头开始创建活动源。 以下是我所做的尝试之一。 我将逻辑直接添加到索引操作中: class ActivitiesController < ApplicationController def index @activities = Activity.order("created_at desc").where(current_user.following_ids) @activity = Activity.find(params[:id]) @commentable = @activity @comments = @commentable.comments @comment = Comment.new end end 它告诉我: ActiveRecord::RecordNotFound in ActivitiesController#index Couldn’t find Activity with ‘id’= 我试图直接在活动索引中呈现注释部分: 评论/意见和评论/表格 Comment activity.rb class Activity < ActiveRecord::Base belongs_to :user has_many :comments, […]

Rails – 如何显示预订确认详情

我目前正在使用rails构建一个Events应用程序,我在预订确认方面有点挣扎,以及如何向预订参加活动的用户展示。 这是我在bookings_controller中的代码 – class BookingsController < ApplicationController before_action :authenticate_user! def new # booking form # I need to find the event that we're making a booking on @event = Event.find(params[:event_id]) # and because the event "has_many :bookings" @booking = @event.bookings.new(quantity: params[:quantity]) # which person is booking the event? @booking.user = current_user end def create # […]

如何将路线映射到Sinatra的控制器?

我想使用Sinatra创建一个简单的实验性MVC框架。 我想按名称”pages”定义资源,例如应该解析为: /pages (index) /pages/new /pages/:id/show (show) 如同映射到app/controllers/PagesController.rb ,相应的get(‘/’)负责索引, post(‘/pages/create’)负责创建等。 在阅读官方文档之后,我甚至感到非常困惑。 我想我需要使用非经典的Sinatra模型,但有人能指出我正确的方向吗? 谢谢

Link_to有两个参数不起作用

在组织的页面上,我有一个部分显示具有该组织的主持人角色的用户。 在每个用户/主持人后面都有一个删除该角色的链接。 但是,此链接无效。 单击它时,没有任何反应。 在浏览器的url栏中,它只显示新文本: https://url/organizations/ek99?data[confirm]=Are+you+certain%3F&data[method]=post&data[organization_id]=100&data[stakeholder_id]=113&data[url]=%2Fremovemoderator%2Fek99 。 有没有人可能会看到这段代码有什么问题? 在组织控制器中 : def show @organization = Organization.friendly.find(params[:id]) @moderators = User.with_role(:moderator, @organization) end 视图页面使用调用部分 。 部分包含每个主持人的链接: 路线 : post ‘removemoderator/:id’=> ‘users#remove_moderator’, as: ‘removemoderator’ 用户控制器方法 : def remove_moderator @organization = Organization.friendly.find(params[:id]) remove_modrights(@organization) end 用户模型方法 : def remove_modrights(organization) self.remove_role :moderator, organization end

如何使用“link_to”而不是“checkbox”进行POST?

这个想法是,当用户有missed_day (又名罢工)时,他会点击此按钮。 他目前可以点击一个复选框来标记missed_day 。 习惯/节目 <label id="” class=”habit-id”>Strikes: <label id="” class=”habit-id-two”>Strikes: = (index + 1) %> <p data-submit-url="” data-delete-url=””> <label id="” class=”level-id”>Level : <label id="” class=”level-id-two”>Level : 0, {class: “habit-check”} %> 1, {class: “habit-check”} %> 2, {class: “habit-check”} %> habit.js $(document).ready(function() { $(“.habit-check”).change(function() { var submitUrl = $(this).parents(“p”).data(“submit-url”); var deleteUrl = $(this).parents(“p”).data(“delete-url”); if($(this).is(“:checked”)) { $.ajax( […]