Rails如何将文本框的名称输入数据库而不是布尔值?

时间:2016-03-11 19:00:45

标签: ruby-on-rails

I am new to rails and to stackoverflow.

问题是我想将文本插入数据库(也是电子邮件)而不是bool。

点击甚至是c#之类的东西。如果checkbox1.checked = true变量string =“复选框的名称在这里很好”。或者任何能够获得字符串的东西(如果有帮助的话,恰好是复选框名称)到DB中(字段是一个字符串)。 这是视图文件。

<div id="home">
  <div id="home-content">
    <div id="home-text-wrapper">
      <div id="home-text">
        <h1 class="cover-heading" id="cover-heading"> Turning Data     Into Value.</h1>
     <p id="opener">Strategic consulting and decision making  solutions.    </p>
      </div>
    </div>
  </div>
</div>

<div class="contact-row">
    <div class="col-md-12" >
       <h3>Contact Us</h3> 
            <p>Please fill out the form below to start the conversation. We will get back to you as soon as we can!</p>
                <%= form_for @contact do |f| %>
                    <% if @contact.errors.any? %>
        <div class="error_messages">
            <h2>Form is invalid</h2>
            <ul>
                 <% for message in @user.errors.full_messages %>
                <li><%= message %></li>
                    <% end %>
            </ul>
        </div>
                    <% end %>
            <p>
                <%= f.label :email, 'Your Email' %><br />
                <%= f.text_field :email %>
            </p>
            <p>
                <%= f.label :first_name, 'First Name' %><br />
                <%= f.text_field :first_name %>
            </p>
        <p>
          <%= f.label :last_name, 'Last Name' %><br />
          <%= f.text_field :last_name %>
        </p>
      <P>
            <%= f.label :linkedin_profile, 'Linkedin Profile' %><br />
            <%= f.text_field :linkedin_profile %>
      </P>
      <P>
            <%= f.label :business_name, 'Business Name' %><br />
            <%= f.text_field :business_name %>
      </P>
       <P>
            <%= f.label :business_website, 'Business Website' %><br />
            <%= f.text_field :business_website %>
      </P>
         <p>I want to learn more about...</p>
         <p>
           <%= f.check_box :extracting_big_data %>
           <%= f.label :extracting_big_data, "Extracting value from my Big Data" %>
         </p>
         <p>
            <%= f.check_box :gain_advantage %>
            <%= f.label :gain_advantage, "Gaining advantage over my competitors" %>
         </p>
         <p>
            <%= f.check_box :bring_new_customers %>
            <%=f.label :bring_new_customers, "Bringing in new customers" %>
        </p>
        <p>
          <%= f.check_box :offer_products_services %>
          <%= f.label :offer_products_services, "Offering new products/services to existing customers" %>
        </p>
        <p>
            <%= f.check_box :reduce_cost %>
            <%= f.label :reduce_cost, "Reducing costs" %>
        </p>
        <p>
          <%= f.check_box :better_visablity_small_business_runs %>
          <%= f.label :better_visablity_small_business_runs, "Getting better visibility into how my small business runs" %>
        </p>
        <p>
             <%= f.check_box :improve_cash_flow  %> 
             <%= f.label :improve_cash_flow, "Improving cash flow" %>
        </p>

        <p>
            <h4>Message</h4>
        </p>
        <p>
            <%= f.text_area :message %>
        </p
           <a href="mailto:thisemail@yahoo.com?suibject wanting info" >              <%= f.submit "Submit" %></a> 
        </p>

    <% end %>
   </div>
   </div>

我的控制器是......

class ContactsController < ApplicationController

 def create
 params.permit!
 @contact = Contact.new(params[:contact])
   if @contact.save
    redirect_to "/contacts", :notice => "Thanks for your messsage."
   else
    render "new"
   if @message.save
   ContactMailer.message(@message).deliver_now
   redirect_to "/message", :noitce => "Thanks for your email."
  end

 end

end


def contact

email = params["email"]
#  first_name = params["first_name"]
end
def index
 @contact = Contact.new
end

def new     
   @contact = Contact.new
   email = params["email"]
end
 def improve_cash_flow 

  @improve_cash_flow = "improve_cash_flow"

 end
 def show
 # @contact = Contact.find(params[:id])
 if @contact.save
 flash[:notice] = 'Thanks for Your Message.'
 format.html {redirect_to root_path}
end
end

def set_contact
@contact=Contact.find(params[:id])



end
private
  def contact_params
   params.require(:contact).permit(:email, :first_name, :last_name,   :linkedin_profile, :business_name, :business_website, :message)
end

此时我即将放弃将复选框名称输入数据库的想法。任何帮助,将不胜感激!  提前谢谢!

1 个答案:

答案 0 :(得分:0)

不幸的是,RoR没有像Webforms那样与服务器通信,因此可能无法实现您想要的解决方案。

Rails的方法是将form_for @contact形式与f.check_box形式取消关联,方法是将check_box_tag转换为def create ... if params[:extracting_big_data] contact.extracting_big_data = "extracting_big_data" end ... other_assignements = "other_values" if other_checks ... more_assignments ... if contact.save ... else ... end end ,并在控制器中执行以下操作:

string

假设您的extract_big_date列已经是permit!类型。

另外,我认为你不应该-Djava.security.egd=file:/dev/./urandom

希望这有帮助!