Tag: ruby on rails 3

如何在控制器的动作中定义的实例变量@variable可以从其视图中调用?

可以从视图中调用控制器操作中定义的变量和变量@variable吗? 喜欢 class UsersControllers def index @whythiscolavery=User.all end end 现在在/views/users/index.html.haml中,为什么变量@whythiscolavery可以在controllers动作中定义的视图中直接访问? 这个查询在重要的采访中让我失望了.. @whythiscolavery.each do user.name end

在设计中注册后不允许登录

我正在使用设计进行身份validation,设计在注册后自动登录,我需要注册但不能登录。有类似的问题链接,但它没有帮助我

Ruby on Rails教程第10章练习RSpec失败

我正在研究Rails教程第10章中的练习,并通过练习让我确保管理员用户无法自行删除。 我最初的想法是简单地检查当前用户的id并将其与params [:id]进行比较,以确保它们不相等。 我在Users控制器中的销毁操作如下所示: def destroy if current_user.id == params[:id].to_i flash[:notice] = “You cannot delete yourself.” else User.find(params[:id]).destroy flash[:success] = “User destroyed.” end redirect_to users_path end 当我在应用程序中手动测试它时,这非常有效,但是我的RSpec测试中有3个失败并出现相同的“未定义方法’to_i’”错误(如下所示): 1) UsersController DELETE ‘destroy’ as an admin user should destory the user Failure/Error: delete :destroy, :id => @user NoMethodError: undefined method `to_i’ for # # ./app/controllers/users_controller.rb:48:in `destroy’ # […]

具有相同角色的用户如何访问具有不同权限的不同post?

我使用设计进行身份validation,并为每个用户分配了一个角色。 我还有一个模型posts ,其中包含多个post。 我想限制角色employee一个用户查看特定post和同一个用户来查看和创建不同的post。 我怎样才能做到这一点?

Devise的控制器在哪里? 如何在用户/编辑页面中添加其他模型的数据?

它似乎设计不在app / controller文件夹中创建任何控制器文件,现在我希望在用户/编辑视图中显示来自其他模型的一些自定义信息,但由于没有控制器要添加,因此无法弄清楚如何操作 @num_of_cars = current_user.num_of_cars.all 我希望在用户/编辑页面中显示num_of_cars,但无法弄清楚如何操作 编辑 – 下面提到的代码给我错误,我把这个代码放在我的设计/注册/编辑文件中 其中myinfo是另一个具有部分_cars_list.html.erb的资源,/ myinfo scaffold工作得非常好但是当我试图将那部分显示给用户/编辑时,它给了我 undefined method `each_with_index’ for nil:NilClass (我使用each_wit_index来显示列表,但我不认为这是问题)

基于选择框选择显示/隐藏文本字段

以简单的forms: “control-label” %> 我正在尝试学习如何使用JQuery仅在“源”字段中选择值“其他”时显示“source_other”文本字段。 从我在网上看到的,看起来我需要使用这样的东西: $(“#source”).change(function() { if ($(“#source”).val()==”Other”) $(“#source_other”).show(); else $(“#source_other”).hide(); }); 但是,我不太了解如何将JQuery与我的表单集成。 有人可以指出我正确的方向吗? 更新: 以下是生成的html代码段: How did you find out about us? — Please select — Source 1 Source 2 Other Specify Other Source

我该如何修复这个创建function?

我有一个函数将嵌套属性拆分成一个数组,以便检查它们的唯一性,然后保存它们。 问题是,我得到一个Can’t mass-assign protected attributes: _destroy错误,因为nested_form gem插入一个hidden_​​field来突出显示应删除哪些属性。 这是* Posts_Contoller.rb *创建函数: def create location_set = params[:post].delete(:locations_attributes) unless params[:post][:locations_attributes].blank? @post = current_blogger.blog_posts.new(params[:post]) @post.locations = Location.find_or_initialize_location_set(location_set) unless location_set.nil? if @post.save redirect_to @post, notice: ‘Blog post was successfully created.’ else render action: “new” end end 这是location.rb中的函数: def self.find_or_initialize_location_set(location_set) #create a locations array locations = [] locations = locations.delete_if { […]

如何删除rails中的多个已检查记录?

我想出了如何显示每行的复选框。 问题是我无法找到如何编写form_tag和submit标签,以便使用detele操作将checked rows参数传递给messages_controller。 以及删除操作中要写的内容。 请帮帮我! 我的观点是 delete ID Read Date Sender Subject 和控制器应该是这样的(根据这里https://github.com/frodefi/rails-messaging/blob/master/app/controllers/messaging/messages_controller.rb ) def trash conversation = Conversation.find_by_id(params[:id]) if conversation current_user.trash(conversation) flash[:notice] = “Message sent to trash.” else conversations = Conversation.find(params[:conversations]) conversations.each { |c| current_user.trash(c) } flash[:notice] = “Messages sent to trash.” end redirect_to messages_path(box: params[:current_box]) end route.rb Example::Application.routes.draw do root :to => “top#index” […]

Rails:资源可以从两条路线到达,还是更好的方法?

我正在开发一个用户可以分享照片的应用。 照片可以选择属于集合,但不必。 目前,用户可以通过以下方式查看所有照片: photos/id 。 我认为如果他们可以通过collections/id/photos浏览特定馆藏的照片也是有意义的 因此,这意味着照片既是顶级资源又是嵌套资源。 我想我可以在这样的路线中设置它: resources :photos resources :collections do resources :photos end 这是一个好主意,还是有更好的方法来重用照片模型,同时还允许它在适当时作为嵌套在集合中? 我非常感谢关于处理这种情况的“轨道方式”的建议。 谢谢!

如何不仅获得单个记录,还获取属于特定搜索查询的所有记录

我写了这个搜索查询,只返回一条记录: @messages = Message.find_by_sender_username(@user.username) # 虽然在我的消息模型中我有两条记录,其中包含sender_username“emmanuel” # # 我认为问题在于模型之间没有真正的关系: 管理员用户名:string 用户名:string 信息 SENDER_USERNAME:字符串 reciepent_username:字符串 身体:字符串 阅读:布尔 MyQuestion是我应该如何写这些模型之间的关系,我为这种关系注意了关于forgein_key的一些事情,但我不是100%肯定如何利用它!