小编Joe*_*Joe的帖子

Rails不可编辑的文本字段

我有一个以下列方式编写的form_for:

<div class="field">
    <%= location.label :city %>
    <%= location.text_field :city, :disabled=>true%>
</div>
<div class="field">
    <%= location.label :country %>
    <%= location.text_field :country, :disabled=>true%>
</div>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,2文本字段被禁用,因为它们是由jquery函数自动填充的,我不希望让用户处理它们.问题是,通过这种方式,视图不会将该参数传递给控制器​​,因为已禁用!有没有其他方法可以将不可编辑的text_field传递给控制器​​,注意我不想使用隐藏字段,因为我想在文本框中向用户显示结果

TNX

ruby-on-rails textfield form-for

77
推荐指数
1
解决办法
5万
查看次数

link_to更新(没有表格)

我想要一个更新资源的链接,而不使用HTML表单.

路线:

resources :users do
resource :profile, :controller=>"profiles"
 resources :friends
end    
Run Code Online (Sandbox Code Playgroud)

耙路线:

 user_friend GET /users/:user_id/friends/:id(.:format){:action=>"show", :controller=>"friends"}
             PUT /users/:user_id/friends/:id(.:format){:action=>"update", :controller=>"friends"}
Run Code Online (Sandbox Code Playgroud)

我想通过简单的链接使用put来更新朋友,如下所示:
<%= link_to"Add as friend",user_friend_path(current_user,:method =>'put')%>

但是当Rails遇到这个链接时,他试图进入节目动作.
问题是:这样做的正确链接是什么?

routes ruby-on-rails put link-to

21
推荐指数
1
解决办法
2万
查看次数

验证器,密码确认

我无法弄清楚为什么模型不会检查密码确认,这是模型的代码:

class User < ActiveRecord::Base
  attr_accessor :password_confirmation
  validates :email, :presence =>true,
                    :uniqueness=>true
  validates :password, :presence =>true,
                    :length => { :minimum => 5, :maximum => 40 },
                    :confirmation =>true
  validates_confirmation_of :password
end
Run Code Online (Sandbox Code Playgroud)

控制器用于从视图中获取数据并尝试执行保存,这是视图的代码:

<h1>Registration process</h1>
<%= form_for(@new_user) do |f|%>
<% if @new_user.errors.any? %>
  <div id="errorExplanation">
    <h2><%= pluralize(@new_user.errors.count, "error") %> prohibited this article from being saved:</h2>
    <ul>
    <% @new_user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
  <% end %>
    <%= f.label :email %><br />
    <%= f.text_field :email %><br …
Run Code Online (Sandbox Code Playgroud)

validation password-confirmation ruby-on-rails-3

18
推荐指数
1
解决办法
2万
查看次数

在rails 3中模拟用户的消息

我已经构建了以下模型来处理用户的消息交换:

 create_table "messages", :force => true do |t|
    t.integer  "source_id"
    t.integer  "destination_id"
    t.string   "object"
    t.string   "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
Run Code Online (Sandbox Code Playgroud)

这些是它的联想:

class Message < ActiveRecord::Base
  belongs_to :sender, :class_name=>'User', :foreign_key=>'source_id'
  belongs_to :reciever, :class_name=>'User', :foreign_key=>'destination_id'
end
Run Code Online (Sandbox Code Playgroud)

而这些是另一方的关联(用户模型):

 has_many :sent_messages, :class_name=> 'Message', :foreign_key=>'source_id', :dependent=>:destroy
  has_many :recieved_messages, :class_name=> 'Message', :foreign_key=>'destination_id', :dependent=>:destroy
Run Code Online (Sandbox Code Playgroud)

该模型是正确的并且正常工作,实际上从我可以检索的消息中,谁是发送者,谁是接收者,从用户,我可以获得所有发送和接收的消息.不幸的是,它没有处理任何情况:如果接收者或发送者删除消息怎么办?消息是独一无二的,所以它在双方消失(坏事).如何知道其中一方是否已阅读该消息?有什么建议吗?你认为我必须重新计划吗?TNX

messaging model ruby-on-rails

10
推荐指数
1
解决办法
3086
查看次数

资本化f.text_field

我有一个form_for,我希望x.textField中的任何值都以Upcase中的第一个字母出现(我在谈论编辑,其中文本字段由db值预先填充).

ruby-on-rails capitalization form-for

7
推荐指数
2
解决办法
4814
查看次数

嵌套form_for

我以这种方式组织项目:用户是主要资源,每个用户都有一个配置文件,每个配置文件有一个位置如下:

 resources :users do    
    resource :profile, :controller => "profiles" do
      resource :location
end
Run Code Online (Sandbox Code Playgroud)

现在我必须构建一个表单来插入所有的配置文件信息,但也包括位置信息(地址等).如果我写下面的代码,它不会关心位置.

<%= form_for(@profile, :url=>{:action=>'update'}, :html => {:multipart => true}) do |f| %>
Run Code Online (Sandbox Code Playgroud)

有人对这种情况有什么建议吗?

TNX

resources ruby-on-rails form-for

2
推荐指数
1
解决办法
7494
查看次数