I have a very small application I ma building in rails. It is a simple weight tracker app. I have created a User model which has a sign up page. Once the user logs in they are redirected to the user#show view. Here is the user controller so far:
class UsersController < ApplicationController
before_filter :authenticate_user!
def show
#user = current_user
end
end
I have 2 other models one is a Weight model and the other a Goal model, I would like it so what when a user signs up they are presented with a screen asking them to type in the current weight and goal weight this information will then be store in the Weight and Goal models respectively along with the logged in users ID.
So far I have been able to add a form to the user show.html.erb template :
<%= form_for #user do |f| %>
<%= f.fields_for :weight do |builder| %>
<fieldset>
<%= f.label :value, "Current weight" %><br />
<%= f.text_field :value %><br />
</fieldset>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Which renders the form correctly but when I then click on the submit button it simply goes to an error page saying Unknown action- The action 'update' could not be found for UsersController. Im assuming iM doing something wrong as it should try and send to the create action.
Is there anyone out there who could help me back on the right path, Im very much a noob at rails.
Well this has nothing to do with all your models. This pertains to the fact you have not defined an update method in your controller.
When you have done that look into accepts_nested_attributes_for if you want to nest models.
Besides all that, a show page usually shows a read only for of the object. An edit page has the editable form of the object.
You are using the form_for Rails helper and passing #user to it, because #user is a persistent model (saved in the db) then the generated form will have the action to /users/:id with PUT method so the request will be sent to an action named update in your UsersController, it seems that you don't have that action defined in your UsersController
it should be somthing like the following:
def update
#user = Users.find(params[:id])
if #user.update_attributes(params[:user])
# do something if saving succeeds
else
# do something if saving fails
end
end
I believe, after searching for this question having similar issues, that it is not update that is missing but edit.
I know this is an old thread, and you have probably solved the issue, but if not try adding this:
def edit
#user = User.find(params[:id])
end
Related
I have a User model, and I am hoping to have different pages to edit their info so that I don't have everything on edit.html.erb
So far I have created views for page_content.html.erb and donation_options.html.erb which have my forms on them. Both work as intended and successfully update my User, but after submitting the form I am redirected the User root, which is show.html.erb. I would like to set it up so that the user stays on the current page after updating their profile. I am not sure if I need to edit my UsersController, or create a new controller, or take a different approach.
Here's an example of my form:
<%= form_for #user do |f| %>
<!--Some inputs-->
<div class="actions">
<%= f.submit 'Update', :class => "btn btn-primary" %>
<!--After submitting, I'm taken to /users/:id/. I'd like to stay on /users/:id/current_view-->
</div>
<% end %>
If you already have a UsersController you should set something up comparable to this:
Class UsersController < ApplicationController
def update
#user = User.find(params[:id])
if #user.update(user_params)
redirect_to your_path_here
end
end
end
I am not sure exactly what your UsersController looks like, but that should give you an idea. After a user is updated, use redirect_to with your path you would like the User sent to. You may also use redirect_to :back if you are looking to redirect back to the current page you are on.
Please see this existing SO post.
to start out preemptively, I've already looked at various similar articles dealing with this, but I still get the error.
I'm starting out on rails and attempting to create a GPA calculator and tracker application for fun (and spent a lot of time searching through documentation); I have a singular controller and view since redirecting to an entire different page for calculating or saving a new GPA every time would look ugly.
Rails will display everything without error up until I add the form, no other erb is written currently, and the form is meant to submit letter grade values from the "f.selection" tag.
The culprit is #cgpa in <%= form_for #cgpa do |f| %>.
My form from main\index view:
<%= form_for #cgpa do |f| %>
<div class="field">
(...)
</div>
<div class="actions">
<%= f.submit 'Calculate' %>
</div>
<% end %>
My controller:
class MainController < ApplicationController
def index
##cgpa = CurrentGpa.all #currently calls a key_to error while form exists, otherwise no error is raised
#pgpa = PastGpa.all
#csem = CurrentSemester.all
#psem = PastSemester.all
end
def new
#cgpa = CurrentGpa.new
end
def create
(...)
end
end
The routes are simply Rails.application.routes.draw { root 'main#index'; resources :main }
If any other information is needed, just let me know to add >.>
When you use: <%= form_for #cgpa do |f| %> this automatically tries to submit the form to the CurrentGpasController create action and for doing so it sends a request to current_gpas_path. So you don't have this path in routes that is why it throwing error. Either you can add routes for CurrentGpa like:
resources :current_gpas
or you can specify a path in the form_for:
<%= form_for #cgpa, url: any_path do |f| %>
So this will submit your form to that url specified.
If you add the current_gpas routes then do create the controller and action to process your input.
And as mentioned in comments do add the #cgpa = CurrentGpa.new this in index action. The above will solve your error you are getting after that.
Hope this helps.
I have two controller in my application events and registrations. I want to send event_id of current event to registrations controller after form submission in event show template
show.html.erb
<%= form_for #registrations do |f| %>
<%= f.submit %>
<% end %>
registrations_controller
class RegistrationsController < ApplicationController
def create
#event = Event.find(params[:event_id])
end
end
but it gives me following error
cannot find event with id =
What i think that since event and registrations are not related in any way.So its giving this error.
How to solve this error
Without knowing more about the model associations and also what the #registrations object is, this is the easiest solution:
Add a hidden field to your form:
<%= f.hidden_field( :event_id, #event.id ) %>
# assumes #event exists in the show action somewhere
and in your controller:
def create
#event = Event.find(params[:registrations][:event_id])
end
If your models are associated you can do something like:
<%= form_for #event.registrations.new %>
but it's hard to say if that will work in your app based on the code provided.
I'm using fields_for to create a page for editing multiple user objects in my app. I've been using this Railscast video as a point of reference. http://railscasts.com/episodes/198-edit-multiple-individually The issue I am running into is that rather than using the user id when generating the html, the form builder is inserting the user's name. I have no idea why. Below is my controller, view, and some of the html generated.
View
Manage Your User Licenses
User License Management for <%= #org.title %>
<%= form_tag update_many_user_licenses_path, :method => :put do %>
<% for user in #users %>
<%= fields_for "users[]", user do |user_fields| %>
<div><%= user.name %></div>
<div><%= user_fields.label :active_license %>
<%= user_fields.check_box :active_license %></div>
<% end %>
<% end %>
<%= submit_tag "Mark as Complete" %>
<% end %>
Controller
class UserLicensesController < ApplicationController
before_filter :authenticate_user!
def manage
#org = current_user.organization
#users = #org.users
end
def update_many
#users = User.find(params[:users])
#users.each do |user|
user.update_attributes!(params[:user].reject { |k,v| v.blank? })
end
flash[:notice] = "Updated users licenses."
redirect_to manage_user_licenses_path
end
end
HTML
<div>Tyrel Denison</div>
<div><label for="users_Tyrel-Denison_active_license">Active license</label>
<input name="users[Tyrel-Denison][active_license]" type="hidden" value="0" /><input id="users_Tyrel-Denison_active_license" name="users[Tyrel-Denison][active_license]" type="checkbox" value="1" /></div>
While I'm not familiar with the LinkedIn API Gem, based on our discussion, it seems the to_param method is being overwritten in your User model.
From the comments, this is apparently what your to_param method looks like:
def to_param # TODO USe something better than this plz
self.name.gsub(/\s+/, '-')
end
You reported that rather than using the user id when generating the html, the form builder is inserting the user's name, and as you can see, this is precisely what is happening in that method. Ultimately, fields_for calls to_param, and you don't have a choice in that matter.
I would be quite astonished that the LinkedIn gem required you to override that method, as it would most definitely break any model that used it, as you experienced (and I can't explain why the other developer decided to do that - it's possible they innocently wrote the method without realizing it already exists in the base class)
I can't give you more detailed information without examining the system more closely, however I would suggestion trying to find how/where to_param is being used (assuming it is being called explicitly somewhere), and create a separate method instead.
I have the following form in my /app/views/password_resets/new.html.erb view
<% form_tag password_resets_path do %>
<label><%= t(:email) %>:</label><br />
<%= text_field_tag "email" %><br />
<br />
<%= submit_tag t("reset_password") %>
<% end %>
along with a controller called PasswordResetsController containing a create method:
def create
#user = User.find_by_email(params[:email])
if #user
#user.deliver_password_reset_instructions!
self.notice = t("password_reset_instructions_are_mailed")
redirect_to root_url
else
flash[:error] = t("no_user_found")
render :action => :new
end
end
When I go to /password_resets/new, fill out the form, and submit, the create method is invoked properly, since the PasswordResetsController::create() method is invoked when a POST happens to /password_resets.
However, when I put the form in another view, say, /app/views/test/index.html.erb, fill out the form, an submit, I get
Unknown action
No action responded to index. Actions:
access_forbidden, admin_created?,
check_roles, create, edit, find_order,
included, new, role_requirements,
role_requirements=, title, title=, and
update
Any ideas why transplanting the form is not working?
Turns out the issue was related to SSL. This showed up in the log:
Filter chain halted as [:ensure_proper_protocol] rendered_or_redirected.
The page I had the form on required SSL, and Rails did not like me submitting the form from an SSL page to to a non-SSL one. So, as a workaround, since I'm using ssl_requirement, I just put
ssl_required :all
in the password_resets controller, and now things work.