【发布时间】:2015-04-30 01:24:22
【问题描述】:
在我的创建新文章页面上,我有一个用于不同选项的选择下拉菜单。根据用户选择的选项,我想设置另一个属性的值。
无法找到执行此操作的最佳方法。我会在新视图中写一个 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
【问题讨论】:
-
听起来像
Javascript。您是否尝试过使用jQuery执行此操作?另外,您的意思是要为:ratingcolor分配字符串“green”、“yellow”等? -
是的,这将是一个字符串。 javascript 是最好的方法吗?
-
是的,我会写在
jQuery。你熟悉吗? -
不幸的是我不是,我必须弄清楚这一点。
-
当然。我很感激
标签: ruby-on-rails forms ruby-on-rails-4 attributes simple-form