我正在尝试嵌套资源,而不是锻炼

时间:2014-01-18 20:26:07

标签: ruby-on-rails ruby

我收到此错误

页面#home中的ActionController :: UrlGenerationError 显示C:/ Users / jhakas realstate / Desktop / call / app / views / layouts / application.html.erb第55行:

没有路由匹配{:controller =>“重新填充”,:action =>“new”}缺少必需的密钥:[:user_id,:wallet_id] 提取的来源(第55行): 52 53 54 55 56 57 58

         <li class="divider-vertical"></li>
         <% if user_signed_in? %>
           <li><%= link_to "Dashboard", new_user_wallet_refill_path %></li>
           <li><%= link_to "Edit Profile", edit_user_registration_path %></li>
           <li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
         <% else %>

和路线中的资源

resources :users do  
  resources :wallets do     
    resources :refills
  end
end

我的钱包控制器

def create
  @wallet = Wallet.new([:user_id]wallet_params)

  respond_to do |format|
    if @wallet.save
      format.html { redirect_to @wallet, notice: 'Wallet was successfully created.' }
      format.json { render action: 'show', status: :created, location: @wallet }
    else
      format.html { render action: 'new' }
      format.json { render json: @wallet.errors, status: :unprocessable_entity }
    end
  end
end

2 个答案:

答案 0 :(得分:0)

这些行需要父资源的ID才能工作:

<li><%= link_to "Dashboard", new_user_wallet_refill_path %></li>
<li><%= link_to "Edit Profile", edit_user_registration_path %></li>

你应该使用这样的路径:

new_user_wallet_refill_path(@user, @wallet)
edit_user_registration_path(@user) # This one might not need the @user object ... I'm not a devise expert.

答案 1 :(得分:0)

当您将钱包资源嵌套在用户资源中时,我认为它会破坏您在create方法中使用的redirect_to @wallet约定。尝试使用实际路径

redirect_to user_wallet_path(@wallet.user, @wallet)