简体   繁体   English

创建操作的路由错误

[英]Routing error for create action

Iam new to rails ,Iam getting the above error don't know why.i have a link from where Iam getting values and storing in controller Iam是Rails的新手,我收到上述错误不知道为什么。我从Iam获取值并将其存储在控制器的链接

    <%= link_to "Add Stocks " ,userstocks_path(user: 
    current_user,short_code: @stock.short_code,stock_id: @stock.id ? 
    @stock.id : '' ),class: "btn btn-primary"%>

my controller create action 我的控制器创建动作

 def create
     if params[:stock_id].present?
     @userstock = Userstock.new(stock_id: params[:stock_id],user: current_user)
      end            
   respond_to do |format|
   if @userstock.save
    format.html { redirect_to users_my_portfolio_path   , notice: 'Userstock was successfully created.' }
    format.json { render :show, status: :created, location: @userstock }
  end
 end
end

my routes 我的路线

    resources :userstocks,except: [:show,:edit,:update,:index]

Instead of going to post request in userstocks_path its going to index of userstocks_path .and if the remove "except: :index" from routes its giving me template error in console .any solutions 而不是去在userstocks_path中发布请求,而是去到userstocks_path的索引。如果从路由中删除“ except::index”,则会在控制台.any解决方案中给我模板错误

Reason being you need to define method type with link_to as per REST index(method type is 'get') and create(method type is 'post') action both has same routes but the difference is their method type. 原因是您需要根据REST索引使用link_to定义方法类型(方法类型为“ get”)和create(方法类型为“ post”)动作都具有相同的路由,但区别在于它们的方法类型。 your can see it by command rake routes | grep userstocks 您可以通过命令rake routes | grep userstocks看到它rake routes | grep userstocks rake routes | grep userstocks

try this - 尝试这个 -

<%= link_to "Add Stocks " ,userstocks_path(user: 
    current_user,short_code: @stock.short_code,stock_id: @stock.id ? 
    @stock.id : '' ),:method=> :post, class: "btn btn-primary"%>

thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM