我在我的应用程序中使用了这个gem,但我不确定gem的不同实现选项之间的区别:
任何人都可以澄清吗?我知道form_for当你想与模特互动时会使用它,但其他两个呢?
我有点像Rails的新手.我正在写一个couchrest-rails应用程序,所以我没有使用activerecord这个模型.我只是想通了那意味着
form_for(@model)
Run Code Online (Sandbox Code Playgroud)
不行.我正在尝试研究如何使用form_tag - 但大多数示例都不涉及new和create操作.
这是错的:
<h1>New article</h1>
<% form_tag new_article_url(@article), :method => :post do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
Run Code Online (Sandbox Code Playgroud)
因为当我运行我的Cucumber场景时,我得到了这个:
Scenario: Create Valid Article # features/article.feature:16
Given I have no articles # features/step_definitions /article_steps.rb:8
And I am on the list of articles # features/step_definitions/webrat_steps.rb:6
/home/deploy/www/www.trackingplace.com/app/ccc/app/views/articles/new.html.erb:3: warning: multiple values for a block parameter (0 for …Run Code Online (Sandbox Code Playgroud) 我正在创建一个form_tag面板,其中包含特定于控制器操作的信息(复选框).此操作在"routes.rb"中设置如下:
resources :students do
collection do
get :send_student_report_pdf
end
end
Run Code Online (Sandbox Code Playgroud)
当我从link_to调用动作时,此设置工作正常:
<%= link_to "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
Run Code Online (Sandbox Code Playgroud)
但是,当我在a中使用它时form_tag,它一直给我这个错误:
Routing Error
No route matches "/students/send_student_report_pdf"
Run Code Online (Sandbox Code Playgroud)
form_tag我的代码在这里:
<%= form_tag :controller => 'students', :action => 'send_student_report_pdf', :method => 'get' do %>
<%= label_tag "Include columns" %> <br>
<%= check_box_tag "first_name", params[:first_name], checked = true %> <%= label_tag "First Name" %><br>
<%= submit_tag "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
<% end …Run Code Online (Sandbox Code Playgroud) 我在用户之间有一个单向关系模型(用户有很多关系),它保存两个给定用户的ID以及用户a对用户b的昵称.我想使用表单允许用户传入另一个用户的电子邮件地址和昵称.如果存在具有该电子邮件的用户,则在用户之间创建关系.如果没有用户匹配,请使用该电子邮件创建一个新的"幻影"用户,然后建立关系.
我对rails很新,并且首先尝试使用基本的:
= form_for @relationship do |f|
= f.label :email
= f.text_field :email
= f.label :nickname, "Nickname"
= f.text_field :nickname
= f.submit "Submit", class: "btn btn-large btn-primary"
Run Code Online (Sandbox Code Playgroud)
此操作失败,因为关系模型不包含电子邮件属性.然后我尝试了以下内容,认为它可能有效,因为它没有直接引用关系模型.
= form_tag :controller => "relationships", :action => "create" do
= label :email
= text_field :email
= label :nickname, "Nickname"
= text_field :nickname
= submit "Submit", class: "btn btn-large btn-primary"
Run Code Online (Sandbox Code Playgroud)
但是这会引发错误"错误的参数数量(1对2)"
我可以在关系模型中添加一个电子邮件字段,但除了我用它来查找所需用户之外,我不需要它.我的计划是使用传递给关系控制器中的create动作的电子邮件和昵称值来创建关系或创建新用户,然后根据具体情况建立关系.那么如何将任意值传递给控制器动作呢?
我有一个有问题模型的应用程序,当我去创建一个记录时,提交按钮什么都不做.除非我刷新页面并尝试再次添加它,否则没有错误只是根本不执行.当我去更新记录时也会发生同样的情况.
这是我的控制器
class ProblemsController < ApplicationController
include Concerns::Votes
def index
@problems = Problem.all
end
def show
@problem = find_problem
end
def new
@problem = Problem.new
end
def edit
@problem = find_problem
end
def create
@problem = current_user.problems.new(problem_params)
@problem.save
redirect_to @problem
end
def update
@problem = find_problem
if @problem.update_attributes(problem_params)
redirect_to @problem
else
redirect_to @problem
end
end
private
def find_problem
@problem = Problem.find(params[:id])
end
def problem_params
params.require(:problem).permit(:name, :description, :url)
end
end
Run Code Online (Sandbox Code Playgroud)
这是我在new.html上渲染的_form.html.erb部分
<div class="row">
<div class="large-12 columns">
<%= form_for @problem do |f| …Run Code Online (Sandbox Code Playgroud) 在将我的应用程序移植到rails 3.1之前,这个form_for曾经工作过
<div class="form-box" style="padding-left:1em;">
<%
action = @existing_mass.nil? ? "add_to_power_plant": "update_power_plant_substrate";
submit_button_label = @existing_mass.nil? ? 'Add': 'Update';
%>
<%= form_for :substrate_mass, @substrate_mass, :remote => true, :url => { :action => action, :substrate_id => @substrate_mass.substrate } do |f| %>
<div>
<%= f.label :quantity_per_year, "Quantity" %>
<%= f.text_field :quantity_per_year, :size => 5, :onclick => 'this.select();', :value => @substrate_mass.quantity_per_year %>
</div>
<div class="actions" style="float:right;">
<%= f.submit submit_button_label %>
</div>
<br/>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
我花了4个多小时试图弄清楚出了什么问题......我肯定不知道了什么
我收到错误:
错误的参数数量(3个用于2)
请注意,我正在尝试更新不是activerecord对象的变量.它只是一个未存储在数据库中的对象.
希望有人能提供帮助.
干杯
我目前有一个这样的表格:
<% form_for @stem, :html => {:multipart => true} do |f| %>
<%= f.file_field :sound %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这输出(基本上):
<form method="post" id="new_stem" class="new_stem" action="/stems">
<input type="file" size="30" name="stem[sound]" id="stem_sound">
</form>
Run Code Online (Sandbox Code Playgroud)
但是我打算在这里使用jQuery的ajaxForm插件,并希望以JSON格式返回新的词干.我知道如果表单的动作是"/stems.json"这会起作用,但是有一个参数可以放入form_for调用,要求它返回JSON吗?
我试过了
<% form_for @stem, :html => {:multipart => true, :action => '/stems.json'} do |f| %>
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.
我有一些数据与散列中的模型相关联.散列在控制器中生成:@hash.
为此数据创建表单的正确方法是什么?
我想出了以下代码:
<% @hash.keys.each do |key| %>
<div class="field">
<%= f.label key %><br />
<%= text_field_tag "hash_" + key, @hash[key] %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这会生成表单,但它会将每个哈希项创建为表单中的单独变量.这似乎不是提交数据的正确方法.我想将数据作为哈希值返回,并使用它来访问它params[:hash].
做这个的最好方式是什么?
使用Rails 3.07,Ruby 1.9.2.
谢谢.
编辑:我应该说清楚.此代码位于为模型生成的表单内.因此,表单需要提交模型的所有字段,以及上面的哈希.
我有一张表格submit_tag.
我想要设置内容值并使用js弹出窗口确认意图.
以下两者都不会调用确认对话框,但会对link_to标记执行此操作.我究竟做错了什么?
f.submit "Do this", data: {confirm: 'Are you sure?'}
f.submit "Do this", confirm: 'Are you sure?'
f.submit confirm: 'Are you sure?'
Run Code Online (Sandbox Code Playgroud) 我的表格如下:
<%= form_for(@car, :html => {:class => 'form-horizontal' }) do |f| %>
<% if @car.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@car.errors.count, "error") %> prohibited this car from being saved</h2>
<ul>
<% @car.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :model_year %><br />
<% %>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
我希望有一个集合选择从100年前开始的模型年份和未来的一年.我试过了
<%= f.select_year :model_year%>
Run Code Online (Sandbox Code Playgroud)
但它说select_year不是一个有效的方法.试着
<%= f.date :model_year, :discard_month => …Run Code Online (Sandbox Code Playgroud) form-for ×10
ruby-on-rails ×10
actionview ×1
button ×1
date ×1
form-submit ×1
forms ×1
hash ×1
javascript ×1
json ×1
link-to ×1
parameters ×1
routes ×1
simple-form ×1