Tag: 路线

如何使“创建”方法可以从外部使用

这是我的方法 def add @result @log = Log.new(params[:log]) if @log.save result = “success”; else result = “fail”; end render :json => result end 这是我的routes.rb match “/logs/add/:value/:category/:note/:own” => “logs#add” 当我尝试这个url时: http://localhost:3000/logs/add/338/testcat/testnote/testown 并且它正确返回json并且新项目已添加到数据库中,但所有字段(value,category,note,own)都为null。 请帮忙 :( 我已经解决了上述问题,但是 如果我想通过从1个url发送到我的ruby来创建多个对象,该怎么办? 例如: 本地主机:3000 /日志/添加/ 338 / testcat / testnote / testown 上面的请求只会创建1个日志到我的数据库如果我想使用上面的解决方案创建许多日志,我必须为一个日志发送一个URL。 我希望我的ruby能够从1个url创建多个日志。 我的意思是我可以通过只向服务器发送一个URL来创建许多日志。 什么routing,method,url应该是什么? (我听说过“ url/add/param1&&param2 ”的内容?

Rails:使用button_to创建控制器的动作

为什么我不能让我的button_to指定我的控制器的创建动作而不是显示 我已经多次尝试添加:action =>“create”和其他类似的东西到button_to参数 我需要在routes.rb中指定创建路由吗? 如果是这样我该怎么办呢? 当使用该行的方式时,我在加载时遇到此错误: 路由错误 没有路由匹配{:action =>“show”,:controller =>“subscriptions”,:feed_url =>“http://foo.com/rss”} 在routes.rb中我将此作为订阅的唯一引用。 resource :subscriptions

如何在rails命名路由上使用ruby

我定义了这样的嵌套资源 resources :item, :only => [:create, :destroy, :update] do resources :item_image, :only => [ :new, :create, :show , :destroy, :index] end 我的路线看起来像这样(耙路线的输出) item_item_image_index GET /item/:item_id/item_image(.:format) item_image#index POST /item/:item_id/item_image(.:format) item_image#create new_item_item_image GET /item/:item_id/item_image/new(.:format) item_image#new item_item_image GET /item/:item_id/item_image/:id(.:format) item_image#show DELETE /item/:item_id/item_image/:id(.:format) item_image#destroy 我认为输出的第一列是“命名路由”。 我想在我的一个视图中显示/item/:item_id/item_image(.:format)的路径。 item_item_image_index GET /item/:item_id/item_image(.:format) item_image#index 我试过这个: 还有这个 两者都不起作用 我得到“未定义的局部变量或方法`item_images_path / item_item_image_index’”错误

更改网页中的URL

当我从一个标签移动到同一页面中的另一个标签时,我怎么能更改URL地址? 我想在routes.rb或控制器中更改代码? routes.rb中: Rails.application.routes.draw do resources :dashboard, only: [:index] resources :profile do collection do patch ‘update_profile’ patch ‘change_password’ end end devise_for :users, controllers: { omniauth_callbacks: ‘users/omniauth_callbacks’ } root ‘dashboard#index’ end 我当前的url是localhost:3000/profile ,现在当我选择update_profile选项卡时,我需要url为localhost:3000/profile/update_profile而不呈现新页面 控制器代码: class ProfileController < ApplicationController before_action :set_user, only: %i[index update_profile] def index @address = if @user.address @user.address else Address.new end end def update_profile […]

RoR:标记对象完成,将completed_on保存在其他模型中

我正在创建一个动作,用户可以将作业标记为完整。 家庭作业主要存储在家庭作业表中,但homework_student包含completed_on和布尔完整属性: homework_student.rb id :integer not null, primary key # school_user_id :integer not null # homework_id :integer not null # completed_on :datetime # created_at :datetime not null # updated_at :datetime not null # complete :boolean belongs_to :homework, :class_name => ‘Homework’, :foreign_key => :homework_id, dependent: :destroy homework.rb has_many :homework_students, :class_name => ‘HomeworkStudent’, dependent: :destroy 我的尝试…… 在我的家庭作业展示视图中,我希望用户能够点击完成他们的作业,并将日期存储在homework_students中的completed_on属性中。 […]

没有路线匹配固定图像的

当current_user单击pin时,该图像如何保存到current_user ? 按钮: 产出 : pry(main)> Inspiration.first id: 1, user_id: 1, image_file_name: “choose-optimism.png”, image_content_type: “image/png”, image_file_size: 230082, pry(main)> Inspiration.last id: 2, # Creating the issue of the route error & image not rendering user_id: 2, image_file_name: “choose-optimism.png”, image_content_type: “image/png”, image_file_size: 230082, 固定图像的id不同,给出错误: No route matches [GET] “/system/inspirations/images/000/000/002/medium/choose-optimism.png” 工作图像有001 : /system/inspirations/images/000/000/001/medium/choose-optimism.png?1478840469 解决此问题的最佳方法是什么,以便我可以继续传递不同的ID,但使用相同的图像? 调节器 @inspirations = @user.inspirations […]

users_path(user)在使用devise时返回/users.id而不是/ users / id

我发现当我使用users_path(user)它返回/users.id ,其中id是用户的id,但我希望它返回/users/id 。 我的配置routes.rb如下所示。 # config/routes.rb Test::Application.routes.draw do root to: “static_pages#home” resources :users, only: [:index, :show] devise_for :users end

Rails 4路由约束仅包含查询字符串或数字

我正在尝试使用约束来实现这两个路由: get “products/:id”, to: “products#show”, constraints: { id: /\d/ } get “products/:name”, to: “products#search”, constraints: { name: /[a-zA-Z]/ } 第一个路由应使用localhost:3000/products/3等URL触发,最后一个路径应使用localhost:3000/products/?name=juice触发。 不起作用,我搜索了很多关于这个问题,但我似乎找到了Ruby on Rails的第二个或第三个版本的解决方案,现在大多数都被弃用了。 有任何想法吗? 谢谢。

Rails确定两个(或更多)给定URL(作为字符串或哈希选项)是否相等的最佳方法是什么?

我想要一个名为same_url的方法? 如果传入的URL相等,则返回true。 传入的URL可能是params选项hash或strings。 same_url?({:controller => :foo, :action => :bar}, “http://www.example.com/foo/bar”) # => true Rails框架帮助器current_page? 似乎是一个很好的起点,但我想传递任意数量的URL。 作为额外的奖励如果可以传入从比较中排除的参数的散列,那将是很好的。所以方法调用可能看起来像: same_url?(projects_path(:page => 2), “projects?page=3”, :excluding => :page) # => true

Rails – 超类不匹配

使用Rails和控制器inheritance。 我创建了一个名为AdminController的控制器,在/app/controllers/admin/admin_user_controller.rb放置了一个名为admin_user_controller的子类。 这是我的routes.rb namespace :admin do resources :admin_user # Have the admin manage them here. end 应用程序/控制器/管理/ admin_user_controller.rb class AdminUserController < AdminController def index @users = User.all end end 应用程序/控制器/ admin_controller.rb class AdminController < ApplicationController end 我有一个用户模型,我想用管理员权限编辑。 当我尝试连接到: http://localhost:3000/admin/admin_user/ 我收到此错误: superclass mismatch for class AdminUserController