Link_to 一个特定的产品
Link_to a specific product
Rails 的新手,正在尝试弄清楚如何设置一个 link_to
到 user
的特定 property
。所以在我的例子中,property
belongs_to
user
和 user
has_many
properties
。我正在使用 Devise 和 CanCan。只需要将当前 user
重定向到他们的 property
。
现在的问题是它试图重定向到与 user_id
相同的 property_id
。 (例如 user/15 尝试重定向到 'property/15' 即使当我检查控制台时 - 假设用户的 属性 是 property/18 - 它有一个 user_id
15 - 所以创建了一个正确的关联)只是不知道如何让 link_to
转到 属性 18(示例)而不是 15。
感谢任何帮助!
Users/show
<div class="col-mds-6">
<%= link_to "View Property", property_path(@user), :class => "btn btn-primary btn-lg btn-custom" %>
</div>
Controller/Users
class UsersController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
路线:
resources :properties, :users, :orders, :charges
路线:
resources :users do
resources :properties
end
User/show
<div class="col-mds-6">
<%= link_to "View Property", user_property_path(@user, property), :class => "btn btn-primary btn-lg btn-custom" %>
</div>
控制器
class PropertiesController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource
def show
@user = User.find(params[:id])
@property = Property.find(params[:property_id])
end
end
问题在于您的 link 具有 property_path(@user)
,其中 @user
是 User
而不是 Property
的实例。这就是为什么路由中的 :id
具有用户 ID(15),而不是 属性 的(18)
resources :users do
resources :properties
end
#users/show.html.erb
<% @user.properties.each do | property | %>
<%= link_to "View Property", user_property_path(@user, property), :class => "btn btn-primary btn-lg btn-custom" %>
<% end %>
current user
to be redirected to their property
您需要使用以下内容:
#app/controllers/properties_controller.rb
class PropertiesController < ApplicationController
def show
@user = current_user.properties.find params[:id]
end
end
#app/views/users/show.html.erb
<% @user.properties.each do |property| %>
<%= link_to property.name, property %>
<% end %>
Rails 的新手,正在尝试弄清楚如何设置一个 link_to
到 user
的特定 property
。所以在我的例子中,property
belongs_to
user
和 user
has_many
properties
。我正在使用 Devise 和 CanCan。只需要将当前 user
重定向到他们的 property
。
现在的问题是它试图重定向到与 user_id
相同的 property_id
。 (例如 user/15 尝试重定向到 'property/15' 即使当我检查控制台时 - 假设用户的 属性 是 property/18 - 它有一个 user_id
15 - 所以创建了一个正确的关联)只是不知道如何让 link_to
转到 属性 18(示例)而不是 15。
感谢任何帮助!
Users/show
<div class="col-mds-6">
<%= link_to "View Property", property_path(@user), :class => "btn btn-primary btn-lg btn-custom" %>
</div>
Controller/Users
class UsersController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
路线:
resources :properties, :users, :orders, :charges
路线:
resources :users do
resources :properties
end
User/show
<div class="col-mds-6">
<%= link_to "View Property", user_property_path(@user, property), :class => "btn btn-primary btn-lg btn-custom" %>
</div>
控制器
class PropertiesController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource
def show
@user = User.find(params[:id])
@property = Property.find(params[:property_id])
end
end
问题在于您的 link 具有 property_path(@user)
,其中 @user
是 User
而不是 Property
的实例。这就是为什么路由中的 :id
具有用户 ID(15),而不是 属性 的(18)
resources :users do
resources :properties
end
#users/show.html.erb
<% @user.properties.each do | property | %>
<%= link_to "View Property", user_property_path(@user, property), :class => "btn btn-primary btn-lg btn-custom" %>
<% end %>
current user
to be redirected to theirproperty
您需要使用以下内容:
#app/controllers/properties_controller.rb
class PropertiesController < ApplicationController
def show
@user = current_user.properties.find params[:id]
end
end
#app/views/users/show.html.erb
<% @user.properties.each do |property| %>
<%= link_to property.name, property %>
<% end %>