I try to add multipart upload for Carrierwave in my form, but I have two controllers(admin_posts and posts) in one model(post).So I do not understand how to specify this
_form.html.haml
= form_for [:admin, #post] do |f|
= f.fields_for :photos do |photo_fields|
= photo_fields.file_field :image
= f.text_field :title, class: "form-control", placeholder: "Title"
= f.text_area :body, rows: 12, class: "form-control", placeholder: "Body"
.pull-right
= f.submit "Send", class: "btn btn-success"
how fix?
sorry for my English
Try this
= form_for [:admin, #post], url: your_action_path(#post), html: { multipart: true } do |f|
Related
Im using cocoon to attach files. I need to be able to remove file when editing a question. Im stuck on getting the exact file name to remove when rendering edit
div.edit_question
=form_for #question, remote: true do |f|
= f.label :title, class: 'label_hidden'
= f.text_field :title
br
= f.label :body, class: 'label_hidden'
= f.text_area :body
br
= f.fields_for :attachments do |f|
.nested-fields
= link_to_remove_association "remove #{ NAME HERE }", f
br
= f.submit 'Update'
Case is closed ))
=form_for #question, remote: true do |f|
= f.label :title, class: 'label_hidden'
= f.text_field :title
br
= f.label :body, class: 'label_hidden'
= f.text_area :body
br
- #question.attachments.each do |att|
= f.fields_for att do |f|
.nested-fields
= link_to_remove_association "remove #{ att.file.filename }", f
br
= f.submit 'Update'
I've just started a new app where I want to take a postcode in a form and save it to the database. My problem is that the create action doesn't seem to be being called no matter what I try.
Routes:
root 'postcodes#new'
resources :postcodes, only: [:new ,:create]
Controller: postcodes_controller.rb
class PostcodesController < ApplicationController
def new
#postcode = Postcode.new
end
def create
#postcode = Postcode.new(postcode_params)
if #postcode.save
flash[:success] = 'Success'
else
flash[:error] = 'Error'
end
end
private
def postcode_params
params.require(:postcode).permit(:code)
end
end
Model: postcode.rb
class Postcode < ApplicationRecord
validates :code, presence: true, uniqueness: true
end
View: postcodes/new.haml
.container
%form
%fieldset.form-group
= form_for #postcode do |f|
= f.label :postcode
= f.text_field :code, placeholder: 'Example Postcode', class: 'form-control'
= f.submit 'Submit', class: 'btn btn-primary'
I've attempted to pass more options in the form_for such as the method and action and now I have a feeling it's a routing error.
Any help will be appreciated.
Thanks.
I believe the problem you are experiencing is a result of your HAML.
You do not need to use, nor should you use, a form HTML element outside the form_for method call.
The form_for method will handle generating this HTML element/tag for you.
You have:
.container
%form
%fieldset.form-group
= form_for #postcode do |f|
= f.label :postcode
= f.text_field :code, placeholder: 'Example Postcode', class: 'form-control'
= f.submit 'Submit', class: 'btn ban-primary'
Which outputs an empty <form> element.
You should have:
.container
= form_for #postcode do |f|
%fieldset.form-group
= f.label :postcode
= f.text_field :code, placeholder: 'Example Postcode', class: 'form-control'
= f.submit 'Submit', class: 'btn ban-primary'
That should generate a proper <form> tag with the required action and method attributes populated with the right URL and 'post' so that your create action is called.
When I submit the following form, the get URI is displaying an empty button param...which doesn't hurt anything, but looks ugly.
How can I get rid of it?
Thanks
=simple_form_for :category,
url: new_category_path,
method: :get,
class: "navbar-form navbar-left" do |f|
.input-group
=f.search_field :search,
type: "search",
class: "form-control",
placeholder: "Search for a category!"
=f.hidden_field :parent_id, :value => #category.id
%span.input-group-btn
=button_tag type:'submit', class: "btn btn-search" do
%i.fa.fa-search
the url looks like:
http://localhost:3000/categories/new?utf8=%E2%9C%93&category%5Bsearch%5D=test&category%5Bparent_id%5D=1&button=
Set :name option to nil. That should do the trick.
%span.input-group-btn
= button_tag type:'submit', class: "btn btn-search", name: nil do
%i.fa.fa-search
Want to show different job categories for my jobs board , using the simple form gem I have added the following to my jobs form.
_form.html.erb
<%= simple_form_for(#job, html: { class: 'form-horizontal' }) do |f| %>
<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>
<%= f.input :title, label: "Job Title", input_html: { class: "form-control" } %>
<%= f.input :description, label: "Job Description", input_html: { class: "form-control" } %>
<%= f.input :company, label: "Your Company", input_html: {class: "form-control" } %>
<%= f.input :url, label: "Link to Job", input_html: { class: "form-control" } %>
<br/>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
but when i go to jobs.new.html it generates the following error
NameError in Jobs#new
Showing /Users/neilpatel/Desktop/Rails/jobs_board/app/views/jobs/_form.html.erb where line #3 raised:
uninitialized constant ActionView::CompiledTemplates::Category
<%= simple_form_for(#job, html: { class: 'form-horizontal' }) do |f| %>
**<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>** -<error
<%= f.input :title, label: "Job Title", input_html: { class: "form-control" } %>
Error specifies you don't have Category Model in your application. That's why rails considering Category as constant and throwing this error uninitialized constant. Try add Category Model in you app/models directory.
<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>
Category.all Should be Modelname.all
I'm trying to have Devise users be able to edit from a modal. The problem is, when the modal loads, it doesn't populate the forms with any information. But if I open the modal from /users/edit the form does populate. Anyone know how to go about this?
_edit.html.haml
.row
.small-2.large-4.columns
.small-4.large-4.columns
%h2
Edit #{resource_name.to_s.humanize}
= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { :multipart => true, method: :put }) do |f|
= devise_error_messages!
%div
= f.label :email
%br/
= f.text_field :email
%div
= f.label :company_name
%br/
= f.text_field :company_name
%div
= f.label :branding
%br/
= f.file_field :branding
%div= f.submit "Update"
%h3 Cancel my account
%p
Unhappy? #{button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete}
= link_to "Back", :back
.small-6.large-4.columns
It's rendered from my application/layout like this:
#editProfile.reveal-modal{"data-reveal" => ""}
= render "devise/registrations/edit"
%a.close-reveal-modal ×