I am having trouble using the Ruby Gem paperclip. I followed the instructions in the ReadMe but I cannot seem to get it to actually load my images. Here is my edit form:
<% form_for :user, #user, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :expertise %><br />
<%= f.text_area :expertise, :class => "expertise" %>
</div>
<div class="field">
<%= f.label :occupation %><br />
<%= f.text_field :occupation %>
</div>
<div class="field">
<%= f.label :city %><br />
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<%end%>
Yet when I try to save I keep getting this error: No route matches "/users/4/edit"
What is the problem
The error is telling you that there's no /users/4/edit route. What does your config/routes.rb look like? If there's a line like:
resources :users
Then, try changing that first line to:
form_for #user
Instead of:
form_for :user, #user
Also, I don't see a file_field in there anywhere so I don't think this question is about Paperclip?
Related
I have an attribute called discount_info. I would like to create another field in the devise edit form, so user can edit this information. I would like to have the field be a text_area. I'm not sure how to do this in rails.
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.email_field :address, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<%= f.text_area :discount_info, :class => "", :placeholder => "if you want a placeholder" %>
Just add the attribute in the form and permit it in the configure_permitted_parameters method.
<div class="field">
<%= f.label :discount_info %><br />
<%= f.text_area :discount_info %>
</div>
And in the controller
def configure_permitted_parameters
#other code
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:other_attributes, :discount_info) }
end
I want the user to be able to register in two steps as I have many fields. Ideally first step would be to accept email and password. As user enter it, they can proceed to fill the next step.
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %></br>
</div>
<div class="field">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :gender %>
<span class="option">Male</span><%= f.radio_button :gender, "m" %>
<span class="option">Female</span><%= f.radio_button :gender, "f" %></br>
</div>
<div class="field">
<%= f.label :mobile_no %></br>
<%= f.telephone_field :mobile_no %>
</div>
<div class="field">
<%= f.label :website %>
<%= f.url_field :website %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<% if #validatable %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :skills %>
<%= f.text_field :skills %>
</div>
<div class="field">
<%= f.label :passion %>
<%= f.text_field :passion %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<% end %>
I tried the approach where user enters email and password and after that they are redirected to edit page where they can update other fields.
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
redirect_to edit_user_path(resource)
end
end
routes.rb
devise_for :users, :pro, :amateur, controllers: { registrations: "registrations" }
However, the redirect doesn't seem to work. I also tried after_inactive_sign_up_path_for as I am using confirmable, still not working.
I can't seem to figure out why this could be also I would like to know if there are any other approach without using any gem?
You could use javascript if you're just interested in user interface. Or if it's important that you're saving all the info as you're going, you can have the form split into partials and override the devise registration controller to render those separate partials.
You will also need to add steps that link with the partials in your user model. I think if you used a gem, this would probably be main part that it will do for you.
this is a great railscast on how to have a multistep form without any gems. the only difference is that you'll need to override the create method in devise
Good Morning,
I have two scaffolds person and city.
rails g scaffold person :name, city_id
rails g scaffold city :cityname
and one formular view/people/
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :city_id %><br />
<%= f.text_field :city_id %>
</div>
<div class="field">
<%= f.label :cityname%><br />
<%= f.text_field :cityname%>
</div>
it doesnt go, i have to create an object of city and put the id in the city_id hidden_field and after that after create-button selected all should be saved in the database.
Not too hard or? Who would you make this?
No need to pass city_id.
<%= form_tag url_for(:controller => :your_controller, :action => :some_action, :method => :get do %>
<div class="field">
<%= label_tag :name %><br />
<%= text_field_tag :name %>
</div>
<div class="field">
<%= label_tag :cityname%><br />
<%= text_field_tag :cityname%>
</div>
<% end %>
In controller:
def some_action
city = City.find_or_create_by_cityname(params[:cityname])
person = Person.new(params[:name])
person.city_id = city.id
person.save!
end
I want to create a new user from my form in my new.html.erb file:
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password%>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br/>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Sign Up"%>
</div>
<% end %>
In my users_controller.rb I have this
class UsersController < ApplicationController
def new
#title = "Sign Up"
#user = Users.new
end
And I have this in my routes.rb
resources :users
I keep getting an error: http://d.pr/wJm8 when I go to users/new and I can't figure out what's wrong...help please?
I think there are some plurality inconsistencies. Your model should be named User not Users.
I have a from like this:
<% form_for(#user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.label :role %> <br/>
<%= f.text_field :role%>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
In the database, role is a "Char" field. I want it different from a textfield, the user can select "Teacher", "Student", if the user select "Teacher", the database will store "T", otherwise, it will store "S". How can I do so? It is necessary for me to add a "User role" table in the database, and then make a relationship with the user? But it is necessary to do it this way? thank u.
Ref select and options_for_select
<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>