【发布时间】:2018-06-22 20:05:24
【问题描述】:
我是 Ror 的新手... 我想使用简单的 link_to 直接从我的管理仪表板更新产品属性(:从 False => True 激活)。 当用户在我的应用程序中添加新产品时,我(管理员)检查产品是否符合条件,然后我从我的管理仪表板发布它。 这是我的代码:
pages/admindashboard.html.erb
Articles on waiting list: <strong><%= @notactives.count %></strong>
<% @notactives.each do |product| %>
<ul>
<li><strong><%= link_to product.name, product_path(product) %> <%= product.user.pseudo %></strong> (<%= product.updated_at.strftime("%d/%m/%Y") %>)
<% if product.active? %>
<p>publié</p>
<% elsif product.status? %>
<p>sold</p>
<% else %>
<span class="label label-success"> <%= link_to "publish the article", publish_product_path %></span>
<% end %>
</li>
</ul>
<% end %>
页面控制器:
def publish_product
@product = Product.find(params[:id])
if @product.update(product_params)
@product.active = true
@product.save
redirect_to :admindashboard
end
end
private
def product_params
params.require(:product).permit(:name, :description, :brand, :category, :color, :size, :state, :price, :address, :status, :active)
end
routes.rb
patch '/publish_product' =>'pages#publish_product'
提前寻求您的帮助
【问题讨论】:
标签: ruby-on-rails controller routes attributes link-to