我正在进行以下操作:users_controller_spec中的rspec测试:it"shouldredirecttotheusershowpage"dopost:create,:user=>@attrresponse.shouldredirect_to(user_path(assigns(:user)))end在我的users_controller中,我有以下内容:defshow@user=User.find(params[:id])@title=@user.nameenddefcreate@title="Signup"@user=User.new(params[:user])if
如何确保controller.action_name返回正确的信息?我依靠它在菜单栏中生成用户友好的消息。考虑以下场景。action_name方法在我使用时效果很好:redirect_toedit_profile_path#Igetaction_nameas"edit"inmyApplicationController但当我使用以下内容时,它不会产生我需要的信息:render:action=>"edit"#Theaction_nameshowsas"get"insteadof"edit"有没有一种方法可以使用渲染Action并仍然获得正确的action_name?谢谢,塔布雷兹----
我有一个像这样的before_action方法:defcurrent_user@current_user||=User.find(:id=>session[:id])end然后我调用这样的方法:defget_fooduserfood=Food.find(:id=>user.id)end这很好,但我想添加异常处理。当用户为nil我想使用@current_user:defget_fooduserfood=Food.find(ifuserisniliwanttouse@current_user.id)end当然可以这样写:defget_fooduserifuser.nil?food=Food
我正在尝试建立一个简单的用户和团队多对多关系,但我在将用户添加到团队时遇到了困难。这是我的模型:classUser为了简单起见,我决定不使用嵌套资源。所以这是我获取这些资源的路线:resources:usersresources:teamsresources:memberships,only:[:new,:create,:update,:destroy]在我团队的:show操作中,我在那里放了一个按钮,这样人们就可以转到一个页面来将用户添加到团队中:Integrantes请注意,我将GET参数传递给new_membership_path函数调用,因此当我到达memberships#ne
假设我有一个UsersController,其中包含一个操作#new。在我的路线文件中,我映射了以下内容:match'signup',to:'users#new'现在/signup和/users/new都可以访问此操作。如何将其限制为仅自定义路由。如果有人回答了这个问题,我深表歉意,但我是新手。我已经搜索过,但没有找到答案。可能是因为我不知道如何简洁地表达这一点。 最佳答案 您可以从用户资源中免除new路由,并将其替换为您的自定义路由:resources:users,except:[:new]get'signup',to:'user
我正在为10号Chef编写LWRP。当该资源在其他配方中运行时,如果某些内容已更改,则应将其标记为“updated_by_last_action”。但如果一切都没有改变。updated_by_last_action应该是假的。例如,我有Chef文档http://docs.opscode.com/lwrp_custom_provider.html#updated-by-last-action.该示例将资源模板包装在一个变量中以测试它是否已更改,然后设置updated_by_last_action状态。所以我的代码应该是这样的f=filenew_resource.filenamedoxxx
根据4.2发行说明中的respond_with/respond_to注释...RubyGuides和:I'dliketotakethisopportunitytosplitrespond_with/class-levelrespond_tointoanexternalplugin.I'mgenerallynotafanofthecodethatcomesoutofusingthispattern.Itencouragesmodel#to_jsonandithidesthedifferencebetweenHTMLandAPIresponsesinwaysthatconvolutet
如何在capybara中获取表单的Action?我知道这不是正确的BDD,但表单正在访问应用程序外部的API。检查表单是否已提交可以使用Webmock完成,但在这种情况下,我认为确保正确设置操作URL更简单。 最佳答案 我很确定capybara(至少是2.1.0版)有帮助程序可以使这更容易。在我们的规范中,我们检查这样的表单操作:form_action=find('#form_id')['action']因此您不需要获取Nokogiri原生元素。 关于ruby-on-rails-如何在C
我收到一条奇怪的错误消息,没有提供太多信息,我很困惑。undefinedmethod`'for#router.rbnamespace:userdoresources:phone_numbers,only:[:new,:create]endcontrollers/user/phone_numbers_controller.rbclassUser::PhoneNumbersControllermodels/phone_number.rbclassPhoneNumber在这一行失败:@phonenumber=PhoneNumber.new我的设计模式错了吗?堆栈跟踪并没有说明太多...感谢M
使用50个if-else语句对于一项操作来说是否过于耗费资源?我正在做这样的事情:ifteam.players.count>1assign_team_type(..)elsifteam.players.count>3assign_team_type(..)...etc....end此外,将50个if-else语句放在Controller内的create操作中而不是after_create方法是否更有效?还是改用caseswitch语句或完全避免使用它会更有效?编辑:感谢您的快速回复!该代码用于社区体育锦标赛,根据该队的球员人数分配球队。我正在尝试编写一些东西,根据添加到该团队的玩家数量