[英]Can't Access Variables
I have a Request model that stores the gender, fitness level, and availability of a person. 我有一个Request模型,用于存储性别,适合水平和一个人的可用性。 This object belongs to a User model. 该对象属于用户模型。 What I'm trying to do is pass the user id of the person who originally made the Request to the and sends the :user parameter to the action send_mail on the mail controller when the user clicks the button . 我想要做的是将最初发出请求的用户的用户ID传递给,然后在用户单击按钮时将:user参数发送到邮件控制器上的send_mail操作。 This doesn't work and Rails says unexpected keyword. 这不起作用,Rails说出了意外的关键字。 I'm basically trying to pass the user.id of the Request object to the mail controller send_email action. 我基本上是试图将Request对象的user.id传递给邮件控制器send_email操作。 Here it will use the user_id to find the user and then from there I will find the email of the user. 在这里它将使用user_id查找用户,然后从那里找到该用户的电子邮件。 I will then send the user an email with this variable. 然后,我将向用户发送包含此变量的电子邮件。
Request Controller 请求控制器
class RequestsController < ApplicationController
before_action :authenticate_user!
def index
@requests = Request.all
end
def create
@request = Request.new(request_params)
@request.user = current_user
@request.save
redirect_to requests_path
end
def new
@request = Request.new
end
def destroy
end
private
def request_params
params.require(:request).permit(:gender, :level, :time_available)
end
end
Index.html.erb Index.html.erb
<% provide(:title, "Open Requests") %>
<table class="table table-hover">
<th>Gender</th>
<th>Level</th>
<th>Time Available: </th>
<% @requests.each do |request| %>
<tr>
<td><%= request.gender %></td>
<td><%= request.level %></td>
<td><%= request.time_available %></td>
<td><%= button_to "I want to exercise with you!", {:controller => "mail", :action => "send_email", :user => Request.user } class: "btn btn-primary" %></td>
</tr>
<% end %>
</table>
Mail Controller 邮件控制器
class MailController < ApplicationController
def send_email
@creation_user = :user
@email = @creation_user.email
end
end
Send_Email.html.erb Send_Email.html.erb
<h2><%= @email %></h2>
In your case, you need to extract the user from the params hash: 在您的情况下,您需要从params哈希中提取用户:
def send_email
@creation_user = params[:user]
@email = @creation_user.email
end
Update: 更新:
You should define a nested resource . 您应该定义一个嵌套资源 。
config/routes.rb 配置/ routes.rb中
resources :requests, only: [:index, :new, :create] do
resources :mails, only: [:create]
end
Now update your view to use your new route. 现在更新您的视图以使用新路线。
app/views/requests/index.html.erb 应用程序/视图/请求/ index.html.erb
<%= button_to "I want to work out with you!", request_mails_path(request) %>
Update your controller. 更新您的控制器。
app/controllers/mails_controller.rb 应用程序/控制器/ mails_controller.rb
def create
@email = user.email
end
private
def user
find_request.user
end
def find_request
Request.find(params[:request_id])
end
Rename your view send_email.html.erb
to create.html.erb
将您的视图send_email.html.erb
重命名为create.html.erb
Simply you can try thi by passing user
to the function 只需将user
传递给函数即可尝试
class MailController < ApplicationController
def send_email
@creation_user = params[:user]
@email = @creation_user.email
end
end
In routes config/routes.rb
在路由中config/routes.rb
get "mail/send_mail", to: 'mail#send_mail'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.