【问题标题】:Setting Attribute Value Depending on User Select Rails 4根据用户选择Rails 4设置属性值
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 非常感谢你,伙计。一个问题,我如何在其中获得 :rating 和 :ratingcolor 属性?当我提交表单时,值没有保存..所以在视图页面中什么都没有显示。不过越来越近了!
  • 你的意思是回到你的控制器吗?在您看来,您是否使用像form_for 这样的助手然后在最后提交?也许您可以使用整个视图(或至少是表单)以及控制器代码来更新您的帖子。
  • 一分钟,将更新。是 simple_form_for,最后提交
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 2013-11-08
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 2014-09-19
相关资源
最近更新 更多