I have a model called 'links.' In routes.rb I have defined it with:
resources :links
I have a form in /links/new it looks like this:
<%= form_for :links do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :url %>
<%= f.url_field :url, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit :"Create Link", class: "btn btn-primary" %>
</div>
<% end %>
When I submit it, I get the error No route matches [POST] "/links/new"
even though this is in the list of routes:
new_link_path GET /links/new(.:format) links#new
What is the problem?
Try this:
<%= form_for(Link.new) do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :url %>
<%= f.url_field :url, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Create Link", class: "btn btn-primary" %>
</div>
<% end %>
add NEW route method in controller Controller
def new
#link = Link.new
end
When you will visit to links/new then new route method will call and render links/new template. so add below code in tamplete
<% form_for(:link, #link, :url => {:action => 'create'}) do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :url %>
<%= f.url_field :url, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Create Link", class: "btn btn-primary" %>
</div>
<% end %>
and handle create request in create action of link controller
def create
#link = Link.new params[:link]
if #link.save
redirect_to :action => 'show', :id => #link.id
else
render :action => 'new'
end
end
dont forget to allow params for link in controller.
this is a proper way to create a new record in ror
Related
I'm building an interactive website through Cloud9 using a tutorial online. We are using bootstrap, JavaScript, ruby on rails, html, and scss. However, I am currently stuck. Whenever I click 'submit'...I get a Routing Error page. None of the information is stored in my db.
routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts
end
contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to new_contact_path, notice: "Message sent."
else
redirect_to new_contact_path, notice: "Error occured."
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
contacts/new.html.erb
<div class="container">
<div class="row">
<h3 class="text-center">Contact Us</h3>
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<div class="well">
<%= form_for "contact" do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
</div>
I followed the instructions exactly, and I have no idea what is wrong or what to change. Can someone help before I rip my hair out?
You need to change
<%= form_for "contact" do |f| %>
to
<%= form_for #contact do |f| %>
Full code
<div class="container">
<div class="row">
<h3 class="text-center">Contact Us</h3>
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<div class="well">
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
</div>
I have a rails app where user can assign task to another user by creating task action. At the moment I have the f.collection_select, but as the number of users grows it won't be a good solution. I would like to change it into kinda search form. My problem is that I don't see how I can do this. I have other search forms, but those are for show/index action. How can I do this for the create action?
here is my current code:
<%= form_for ([#user, #task]) do |f| %>
<%= render 'layouts/error_messages', object: f.object %>
<div class="field form-group">
<%= f.label :executor_id %>
<%= f.collection_select(:executor_id, User.all, :id, :email, class: "form-control") %>
</div>
<div class="field form-group">
<%= f.label :content %>
<%= f.text_area :content, class: "form-control" %>
</div>
<div class="field form-group">
<%= f.label :deadline %>
<%= f.date_select :deadline, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Create Task", class: 'btn btn-primary', "data-sid" => current_user.id, "data-rip" => :executor_id %>
</div>
<% end %>
I've looked through several of these kind of questions but none of them work for me.
Weirdly enough the same strategy to a different form works perfectly.
I have model validations in place but when I submit an empty form I get an error of missing template of ceeate where format is HTML.
I guess the purpose of each file is self-explanatory.
My ContactsController.rb:
class ContactsController < ApplicationController
respond_to :html, :js
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:contact])
#contact.request = request
end
end
new.js.ebr:
$("#main-block").html("<%= escape_javascript(render 'contact_form') %>")
_contact_form.html.erb:
<div align="center">
<h3>Sazinieties ar mums</h3>
<ul class="errors"></ul>
<%= form_for #contact, :html => {:class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :name, class: "control-label" %>
<div class="controls">
<%= f.text_field :name %>
</div>
</div>
<div class="control-group"><div class="field">
<%= f.label :e_mail, class: "control-label" %>
<div class="controls">
<%= f.email_field :e_mail %>
</div>
</div>
<div class="control-group"><div class="field">
<%= f.label :message, class: "control-label" %>
<div class="controls">
<%= f.text_area :message %>
</div>
</div>
<div>
</br>
<%= f.submit('Sūtīt', remote: true, class: "btn btn-primary") %>
</div>
<% end %>
</div>
create.js. erb:
<%= render 'save' %>
_save.js.erb:
$("ul.errors").html("")
<% if #contact.errors.any? %>
<% #contact.errors.full_messages.each do |message| %>
$("ul.errors").append($("<li />").html("<%= message.html_safe %>"))
<% end %>
<% else %>
$("#main-block").empty()
$("#main-block").html("Ziņa ir nosūtīta. Tuvākajā laikā sniegsim atbildi!")
<% end %>
You need to add this to your form:
<%= form_for #contact, :remote => true, :html => {:class => 'form-horizontal' } do |f| %>
And remove remote: true from your submit button.
I would like to create custom links into rails - for example I have my users filling out this form when they create a new pin:
<%= form_for #pin, html: { multipart: true } do |f| %>
<% if #pin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#pin.errors.count, "error") %> prohibited this pin from being saved:</h2>
<ul>
<% #pin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :Name_of_your_startup %>
<%= f.text_field :startupname, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :City %>
<%= f.text_field :city, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :Tag %>
<%= f.text_field :tag, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :Reward_for_users %>
<%= f.text_field :reward, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
In my the pins views I want to create custom url to tweet: So I have this url:
http://twitter.com/intent/tweet?text="HERE GOES THE CUSTOM PART"
How could I integrate at the end of this url, for example: <%= f.label :description %>
Any ideas on how I could do such a thing ?
The correct syntax should be
<%= link_to "Link text here", "https://twitter.com/intent/tweet?text=#{#pin.description}" %>
my view is
<h3> Register New Items </h3>
<div class="row">
<div class="span2 offset4">
<%= nested_form_for #item_category, :url => items_path, :method => :post, :html => { :class => "item_category"} do |f| %>
<div><%= f.label :item_name %>
<%= f.text_field :item_name %></div>
</br>
<div>
<%= f.fields_for :item_companies do |f| %>
<%= f.label :company_name %>
<%= f.text_field :company_name %>
</div>
</br>
<%= f.fields_for :item_weights do |f| %>
<div>
<%= f.label :weight %>
<%= f.text_field :weight %>
</div>
</br>
<div>
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<%end%>
<%end%>
<div><%= f.submit "Submit" %></div>
<% end %>
</div>
and controller
def new
#item_category = ItemCategory.new
item_company = #item_category.item_companies.build
item_company.item_weights.build
end
and when I have applied debugger in create action and I saw value of params[:item_category] and this is generated {"item_name"=>"iname", "item_companies_attributes"=>{"0"=>{"company_name"=>"cname", "item_weights_attributes"=>{"0"=>{"weight"=>"20kg", "price"=>"100"}}}}}
ItemCategory.new(params[:item_category])
generated error
ActiveModel::MassAssignmentSecurity::Error Exception: Can't mass-assign protected attributes: item_companies_attributes.
Where i am wrong and how to save value in three tables using this.
It seems that you forgot to add this piece of code in your ItemCategory model:
attr_accessible :item_companies_attributes
which will allow you to set these attributes.