在我的管理面板 rails 中使用 link_to 更新产品属性
update a product attribute with link_to in my admindashboard rails
我是 Ror 的新手...
我想使用简单的 link_to 直接从我的管理仪表板更新产品属性(:active from 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'
提前感谢您的帮助
您可以将路由设置为接受来自参数的 id:
patch '/publish_product/:id' =>'pages#publish_product'
然后在 link_to 中使用该路径加上 id:
<% @notactives.each do |product| %>
...
<%= link_to 'publish the article', publish_product_path(product.id) %>
<% end %>
并且在控制器中,您 "flips" 基于当前值的属性,例如:
def publish_product
@product = Product.find(params[:id])
@product.active = !@product.active
redirect_to :admindashboard if @product.save
end
我是 Ror 的新手... 我想使用简单的 link_to 直接从我的管理仪表板更新产品属性(:active from 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'
提前感谢您的帮助
您可以将路由设置为接受来自参数的 id:
patch '/publish_product/:id' =>'pages#publish_product'
然后在 link_to 中使用该路径加上 id:
<% @notactives.each do |product| %>
...
<%= link_to 'publish the article', publish_product_path(product.id) %>
<% end %>
并且在控制器中,您 "flips" 基于当前值的属性,例如:
def publish_product
@product = Product.find(params[:id])
@product.active = !@product.active
redirect_to :admindashboard if @product.save
end