我正在使用ruby开发一个消息传递应用程序,当前遇到一个无法修复的阻止程序。我一直在使用教程,我认为我找不到解决方案的部分原因是因为这一点。我的应用程序允许用户登录并注册,然后可以添加,查看和编辑联系人。最后,可以将消息发送给不同的收件人。问题是,我无法在联系人中找到收件人并向他们发送消息。我只能选择我的名字作为用户(这不是它的意图)。我已经附加了这里使用的控制器:
contacts_controller
class ContactsController < ApplicationController
def index
@contacts = Contact.all
end
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
flash[:success]= "new contact successfully added!"
redirect_to contacts_path
else
render 'new'
end
end
def edit
@contact = Contact.find(params[:id])
end
def update
@contact = Contact.find(params[:id])
permitted_columns = params.require(:contact).permit(:name, :company, :email, :phone)
@contact.update_attributes(permitted_columns)
redirect_to contacts_path
end
def destroy
@contact = Contact.find(params[:id])
@contact.destroy
redirect_to contacts_path
end
private
def contact_params
params.require(:contact).permit(:name, :company, :email, :phone)
end
end
messages_controller
class MessagesController < ApplicationController
def index
@messages = Recipient.where(:user_id => current_user.id).order('created_at DESC')
end
def new
@message = Message.new
@recipients = Contact.all
end
def create
@message = current_user.messages.build(message_params)
if @message.save
flash[:success]= "Message sent!"
redirect_to contacts_path
else
flash[:alert]= "sorry!message unsent"
render :new
end
end
private
def message_params
params.require(:message).permit(:body, :sender_id, user_tokens:[])
end
end
users_controller
class UsersController < ApplicationController
def index
end
def create
user = User.new(user_params)
if user.save
session[:user_id] = user.id
redirect_to '/contact'
else
flash[:register_errors] = user.errors.full_messages
redirect_to '/'
end
end
private
def user_params
params.require(:user).permit(:fname, :lname, :email, :password, :password_confirmation)
end
end
sessions_controller
class SessionsController < ApplicationController
def create
user = User.find_by(email:login_params[:email])
if user && user.authenticate(login_params[:password])
session[:user_id] = user.id
redirect_to '/contact'
else
flash[:login_errors] = ['invalid username or password']
redirect_to '/'
end
end
def destroy
session[:user_id] = nil
redirect_to '/', notice: 'Successfully logged out!'
end
private
def login_params
params.require(:login).permit(:email,:password)
end
end
_recipient.html.erb由new.html.erb
呈现。这是代码:
<div class="container vertical-center">
<div id ="stream">
<%= form_for :message, url:messages_path do |f| %>
<%= f.text_area :body, id: "url", placeholder: "Message", class: "message_body" %>
<div id="stream-list" class="follow-list">
<ul>
<% @recipients.each do |contact| %>
<label for="user<%=contact.id%>" >
<li id="stream_item">
<span class="list-group-item"><%= contact.name %></span><%= check_box_tag "message[user_tokens][]",user.id, @message.users.include?(user), id: "user#{user.id}" %>
</li>
</label>
<br>
<% end %>
</ul>
</div>
<div class="stream-footer">
<%= f.button :submit, class: "btn btn-success" %>
<% end %>
</div>
</div>
</div>
这是我尝试写消息时出现的错误
答案 0 :(得分:1)
不清楚为什么在视图模板中使用局部变量user
。我认为这只是一个错误,应该使用contact
变量代替
<span class="list-group-item"><%= contact.name %></span><%= check_box_tag "message[user_tokens][]", contact.id, @message.users.include?(contact), id: "user#{contact.id}" %>
还有一个小的HTML错误:ul
标签应包含li
标签;其他标签不允许作为直接后代。所以我还要将该列表重写为:
<ul>
<% @recipients.each do |contact| %>
<li id="stream_item">
<label for="user<%=contact.id%>" >
<span class="list-group-item"><%= contact.name %></span><%= check_box_tag "message[user_tokens][]", contact.id, @message.users.include?(contact), id: "user#{contact.id}" %>
</label>
</li>
<br>
<% end %>
</ul>