在我的创建新文章页面上,我有一个选择下拉列表,用于不同的选项。根据用户选择的选项,我想设置另一个属性的值。
无法找到最佳方法。我会在新视图中写一个if语句吗?还是文章控制器?
这是我的new.html.erb:
<%= f.label "Rating" %><br>
<%= f.select :rating, options_for_select(['A+','A','A-','B+','B','B-','C+','C','C-','D+','D','D-','F+','F','F-'], params[:rating]), {}, { :class => 'span3 controls controls-row' } %>
<%= f.label "Rating color" %><br>
<%= f.text_field :ratingcolor %>
具体来说,当用户选择A +,A或A-时,我希望:ratingcolor自动分配绿色。如果B +,B或B-,则颜色为黄色......依此类推。
如果已经有相关文档,请您指点一下吗?谢谢!
更新
new.html.erb(简化版)
<div class="form-inputs">
<%= simple_form_for(@article) do |f| %>
<%= f.error_notification %>
<select id="rating">
<option value="A+">A+</option>
<option value="A">A</option>
<option value="A-">A-</option>
<option value="B+">B+</option>
<option value="B">B</option>
<option value="B-">B-</option>
<option value="C+">C+</option>
<option value="C">C</option>
<option value="C-">C-</option>
</select>
<label for="rating_color">Rating color</label>
<input id="rating_color" type="text" name="rating_color" />
<% end %>
</div>
由于我使用的是simple_form_for,我尝试了类似的东西
<%= f.label "Rating" %><br>
<%= f.select :rating, options_for_select(['A+','A','A-','B+','B','B-','C+','C','C-','D+','D','D-','F+','F','F-'], params[:rating]), {}, :class => 'span3 controls controls-row', :input_html => { :id => "rating" } %>
<%= f.label "Rating color" %><br>
<%= f.text_field :ratingcolor, input_html => { :id => "rating_color" } %>
但是这没用。
这是我的articles_controller.rb
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, except: [:index, :show]
def index
if params[:query].present?
@articles = Article.search(params[:query], misspellings: {edit_distance: 1})
else
@articles = Article.all
end
if @articles.blank?
return redirect_to request_path
end
get_query
end
def show
@article = Article.find(params[:id])
if request.path != article_path(@article)
redirect_to @article, status: :moved_permanently
else
respond_to do |format|
format.html # show.html.erb
format.json { render json: @article }
end
end
end
def new
@article = current_user.articles.new
end
def edit
@article = current_user.articles.find(params[:id])
end
def create
@article = current_user.articles.new(article_params)
respond_to do |format|
if @article.save
format.html { redirect_to @article, notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
def update
@article = current_user.articles.find(params[:id])
respond_to do |format|
if @article.update(article_params)
format.html { redirect_to @article, notice: 'Article was successfully updated.' }
format.json { render :show, status: :ok, location: @article }
else
format.html { render :edit }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
def destroy
@article = current_user.articles.find(params[:id])
@article.destroy
respond_to do |format|
format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_article
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:isare, :name, :image1, :image2, :image3, :image4, :r1image, :r2image, :r3image, :r4image, :title, :aka, :category, :rating, :ratingcolor, :whyrating, :alternative1, :alternative2, :alternative3, :checkthisout, :nutritiontable, :sugarsdp, :facts, :video1, :video2, :related1, :related2, :related3, :related4, :sources)
end
def get_query
@userquery = params[:query]
end
end
答案 0 :(得分:1)
这是Fiddle。基本上,从select
获取值,然后使用它来设置text field
中的值。请注意,我已使用id=rating
和id=rating_color
作为挂钩,但您可以根据需要更改这些内容(他们只需要在HTML
和JavaScript
之间进行匹配。
这可以在您的视图中一起使用,或者您可以将JS放在app/assets/javascripts
(更好)的单独文件中。只需确保在清单文件中包含jQuery
(我认为默认情况下是在Rails 4 +中)。