根据用户选择轨道设置属性值 4.



在我的创建新文章页面上,我有一个不同选项的选择下拉列表。根据用户选择的选项,我想设置另一个属性的值。

很难找到最好的方法来做到这一点。我会在新视图中编写 if 语句吗?还是文章控制器?

这是我的新.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

这是小提琴。 基本上,从select中获取值,然后使用它来设置text field中的值。 请注意,我使用了 id=ratingid=rating_color 作为钩子,但您可以根据需要更改它们(它们只需要在HTMLJavaScript之间匹配。

这可以放在您的视图中,或者您可以将 JS 放在 app/assets/javascripts 中的单独文件中(更好)。 只要确保你在清单文件中包含了jQuery(我认为它是Rails 4+中默认的)。

最新更新