I'm trying to pass a project id from a session in a hidden field on a task form, so that when the task is created, it has the id of the project that it is assigned to. I've done this before fine, and have even tried copying over the code that I used from when it worked, but changing names and I'm getting errors no matter what I do - if anyone could help point out where I'm going wrong, it would be much appreciated, thanks!
The error I'm getting with this configuration is: "unknown attribute: project_id"
View Code (tasks/_form):
<%= form_for(#task) do |f| %>
<div class="field">
<%= f.hidden_field :project_id, :value => session[:project_id] %>
</div>
...
<% end %>
Model Code (task):
attr_accessible :project_id
belongs_to :project
Controller code (tasks_controller):
def new
#task = Task.new
#project_id = session[:project_id]
respond_to do |format|
format.html # new.html.erb
format.json { render json: #task }
end
end
def create
project_id = session[:project_id]
#task = Task.new(params[:task])
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render json: #task, status: :created, location: #task }
else
format.html { render action: "new" }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
Here's the application trace - it is pointing to line 46, which in my code is the '#task = Task.new(params[:task])' line in the create action...?
app/controllers/tasks_controller.rb:46:in `new'
app/controllers/tasks_controller.rb:46:in `create'
Does the Task model have a project_id column?
Related
Issue: I am looking for a way to have separate notices for different values in a tables column.
I have order_status that has 3 separate values, created, cancelled, and charged.
I would like 3 separate notices for each when the columns are changed from a view.
a view will be something like:
<%= form_for #order, remote: true do |f| %>
<%= f.hidden_field :order_status, value: "cancelled" %>
<%= f.button type:'submit', class: "btn btn-danger" %>
<% end %>
This will change the column to "cancelled".
I then want to create a method like:
def cancel_update
respond_to do |format|
if #order.update(params[:order_update])
if user_signed_in?
if #order.order_status = "cancelled"
format.html { redirect_to #order, notice: 'Order was successfully cancelled.' }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
end
end
But this method didn't work how i planned. When i use this method, nothing happens when i click the button, no page reload, redirect, etc. I get the error: "The action 'update' could not be found for OrdersController"
(This was tested by taking out the original update method - which is below).
Now when i use this update method, it works but doesn't pin point the value update on the order_status only.
def update
respond_to do |format|
if #order.update(order_params)
if user_signed_in?
format.html { redirect_to #order, notice: 'Order was successfully uploaded.' }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
if buyer_signed_in?
format.html { redirect_to #order, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
end
Here is another attempt that somehow didn't function:
def order_cancel
respond_to do |format|
if #order.update(order_status)
format.html { redirect_to #order, notice: 'Order was successfully cancelled.' }
format.json { head :no_content }
end
end
end
private
def order_status
params.permit(:order_status)
end
How can I pin point the order_status values from the controller to allow me to have separate actions and notices when the values are changed by a end user?
Given that you use the Rails form builder to generate the form's HTML
<%= form_for #order, remote: true do |f| %>
<%= f.hidden_field :order_status, value: "cancelled" %>
I would expect that the generated name of the input field is nested under order. That said you will need to follow these nesting when permitting the params:
def order_status
params.require(:order).permit(:order_status)
end
When you are unsure how the parameters really look like you might want to have a look at the generated HTML structure of the form or you can look at the Rails logs for the update request to the application.
So, those three lines are wrong:
if #order.update(params[:order_update])
if user_signed_in?
if #order.order_status = "cancelled"
Should be:
if user_signed_in?
if #order.update(status: params[:order_status])
if #order.order_status == "cancelled"
But actually should be #order.update!(status: :cancelled) in a cancel action, or at least have a state machine to validate that the user is not messing up the states of the orders.
Or like is expected by your form, those should be in a update method (not cancel_update)
The update method you posted doesn't make sense, it has a minimum of 2 renders, I think you meant to not have the buyer_signed_in section.
Can you help a noob, please?
I have 2 models - Player and Poker Tables, which have has_and_belongs_to_many association. When i try create player i catch error
undefined method `poker_table'
respond_to do |format|
**if #player.save**
format.html { redirect_to #player, notice: 'Player was successfully created.' }
format.json { render :show, status: :created, location: #player }
else
I use checkboxes for mark needed poker tables, here form code:
<% #poker_tables = PokerTable.all %>
<% #poker_tables.each do |poker_table| %>
<div>
<%= check_box_tag "player[poker_table_ids][]", poker_table.id %>
<%= poker_table.name %>
<%= poker_table.actual_time %>
</div>
<% end %>
create method and params
def create
#player = Player.new(player_params)
respond_to do |format|
if #player.save
format.html { redirect_to #player, notice: 'Player was successfully created.' }
format.json { render :show, status: :created, location: #player }
else
format.html { render :new }
format.json { render json: #player.errors, status: :unprocessable_entity }
end
end
end
def player_params
params.require(:player).permit(:email, :poker_table_ids => [])
end
I can create poker table, but couldn't create a player with associated poker tables.
I don't really understand what I'm doing wrong. I studied a lot of resources about this theme, but i can't find answer.
I've had a problem over the last few days getting my nested resources to create and display properly. There are tons of similar questions on StackOverflow and lots of blog posts on this, but they all seem to either deal with a older version of Rails or a different issue. I'm at the point where once I finally fix something, another error pops up. I've narrowed it down to me making a stupid mistake or typo being too inexperienced to notice.
I have a Jobs model that belongs to a Venue model. The venue works fine and I've even gotten as far as to be able to go to my nested Jobs index under each Venue and bring up the New and Edit forms, but going to 'Show' or creating a new Job caused an undefined method error. After a lot of searching I found plenty with the same problem and tried to implement their fixes, but now I'm getting a Routing Error.
Most of my confusing comes from when to leave off the #, when to use :venue_id instead of :id in params, etc. Every example I see seems to have a different way and I can't seem to make any of them work for me.
Any bump in the right direction would be extremely helpful.
The Routing Error
No route matches {:action=>"show", :controller=>"jobs", :venue_id=>#<Venue id: 1, name: "Burger Chef", city: "Chicago", state: "Illinois", areacode: 60614, created_at: "2013-02-05 21:33:41", updated_at: "2013-02-06 23:01:05", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>}
routes.rb
Twist::Application.routes.draw do
resources :users
devise_for :users
resources :venues do
resources :jobs
end
end
jobs_controller.rb
class JobsController < ApplicationController
# GET /jobs
# GET /jobs.json
before_filter :get_venue
def get_venue
#venue = Venue.find(params[:venue_id])
end
def index
#jobs = #venue.jobs
respond_to do |format|
format.html # index.html.erb
format.json { render json: #jobs }
end
end
# GET /jobs/1
# GET /jobs/1.json
def show
#job = #venue.job.find(params[:id])
if params[:id]
#venue = Venue.where(:id => params[:id]).first
#jobs = #venue.job_url
else
#jobs = Jobs.all
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: #job }
end
end
# GET /jobs/new
# GET /jobs/new.json
def new
#job = #venue.jobs.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #job }
end
end
# GET /jobs/1/edit
def edit
#job = #venue.job.find(params[:venue_id])
end
# POST /jobs
# POST /jobs.json
def create
#job = #venue.jobs.new(params[:job])
respond_to do |format|
if #job.save
format.html { redirect_to :action => :show, :id => #venue.id,
notice: 'Job was successfully created.' }
format.json { render json: [#venue, #job],
status: :created,
location: [#venue, #job] }
else
format.html { render action: "new" }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
# PUT /jobs/1
# PUT /jobs/1.json
def update
#job = Job.find(params[:id])
respond_to do |format|
if #job.update_attributes(params[:job])
format.html { redirect_to #job, notice: 'Job was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
# DELETE /jobs/1
# DELETE /jobs/1.json
def destroy
#job = Job.find(params[:id])
#job.destroy
respond_to do |format|
format.html { redirect_to jobs_url }
format.json { head :no_content }
end
end
end
venues_controller.rb
class VenuesController < ApplicationController
# GET /venues
# GET /venues.json
def index
#venues = Venue.all
if params[:name]
#user = User.where(:name => params[:name]).first
#venues = #user.venues
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #venues }
end
end
# GET /venues/1
# GET /venues/1.json
def show
#venue = Venue.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #venue }
end
end
# GET /venues/new
# GET /venues/new.json
def new
#venue = Venue.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #venue }
end
end
# GET /venues/1/edit
def edit
#venue = Venue.find(params[:id])
#if session[:user_id] != #venue.user_id
# flash[:notice] = "Sorry, you cannot edit this venue."
# redirect_to(venues_path)
# =>end
end
# POST /venues
# POST /venues.json
def create
#venue = Venue.new(params[:venue_id])
respond_to do |format|
if #venue.save
format.html { redirect_to #venue, notice: 'Venue was successfully created.' }
format.json { render json: #venue, status: :created, location: #venue }
else
format.html { render action: "new" }
format.json { render json: #venue.errors, status: :unprocessable_entity }
end
end
end
# PUT /venues/1
# PUT /venues/1.json
def update
#venue = Venue.find(params[:id])
respond_to do |format|
if #venue.update_attributes(params[:venue])
format.html { redirect_to #venue, notice: 'Venue was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #venue.errors, status: :unprocessable_entity }
end
end
end
# DELETE /venues/1
# DELETE /venues/1.json
def destroy
#venue = Venue.find(params[:id])
#venue.destroy
respond_to do |format|
format.html { redirect_to venues_url }
format.json { head :no_content }
end
end
end
job.rb
class Job < ActiveRecord::Base
attr_accessible :description, :name, :requirement, :venue_id
validates :name, :presence => true, :length => { :minimum => 3 }
belongs_to :venue
end
venue.rb
class Venue < ActiveRecord::Base
attr_accessible :areacode, :avatar, :city, :name, :state
has_many :jobs
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
/jobs/show.html.erb
<p id="notice"><%= notice %></p>
<% #job = Job.find(param[:venue_id]) %>
<p>
<b>Name:</b>
<%= #job.name %>
</p>
<p>
<b>Company:</b>
<p><%= #venue.name %></p>
<p><%= link_to #job.venue.name, venue_path(#venue) %></p>
<p>
<b>Job:</b>
<%= #job.job_id %>
</p>
<p>
<b>Description:</b>
<%= #job.description %>
</p>
<p>
<b>Requirement:</b>
<%= #job.requirement %>
</p>
<%= link_to 'Edit', edit_venue_job_path(#venue, #job) %> |
<%= link_to 'Back', venue_jobs_path(#venue, #job) %>
**/jobs/index.html.erb**
<div class="usergrid">
<% jobs = #venue.jobs %>
<% #venue.jobs.each do |job| %>
<div class = "user venue">
<p>
<h2><%= link_to job.name, venue_job_path(#venue) %></h2>
<%= link_to 'Edit', edit_venue_job_path(#venue, job) %><br/>
<%= link_to 'Delete', venue_jobs_path(#venue, #job), :confirm => 'Are you sure?', :method => :delete %>
</div>
<% end %>
<div style="clear:both"></div>
</div>
<%= link_to 'New Job', new_venue_job_path(#venue) %>
I realize that...
This might be a really obvious fix, but being new to Rails there are definitely still some fundamentals that I don't completely understand.
If I posted too much or too little code please let me know, I'm not entirely sure how much is the least necessary for this.
There might be more than one error, there might be a lot of errors, and many of them could have been caused by me trying to fix this when I really didn't know what I was doing. I wanted to fix this myself, but I can't handle messing with it anymore when I'm really just making it worse.
I've tried messing with the routes, changing the actual links and routes, messing with scope, and as many of the common fixes for these errors that I could find and none of them seemed to help.
Thanks!
The error is saying that there is no route for the given params :
{:action=>"show", :controller=>"jobs", :venue_id=> "an_id"}
You can check that by running rake routes, and you'll see that, as jobs is nested under venue, the jobs#show controller actions needs two parameters : the venue_id (which is the job's 'parent') and the id which is the job id.
I quickly checked your code, and I think that one one of the things that causes the error is this line :
<h2><%= link_to job.name, venue_job_path(#venue) %></h2>
this should be
<h2><%= link_to job.name, venue_job_path(#venue, #job) %></h2>
Basically you'll get that kind of error, whenever you'll try to render a link to a job without providing the venue.
Let me know if you need more details or more information.
In my rails app form, I've the following code for a multi-select:
<div class="field">
<%= f.label :frameworks %><br />
<%= f.collection_select :framework_ids, Framework.all, :id, :name, {}, {:multiple => true} %>
</div>
It works fine at creation, and it shows correctly the previowsly selected frameworks on edit view.
But when I submit some other updated fields, it repeats the frameworks entries in my database.
For example, if I had selected "framework1", "frameworks2", after updating I've in database "framework1", "frameworks2", "framework1", "frameworks2", and if I update one more time: "framework1", "frameworks2","framework1", "frameworks2","framework1", "frameworks2".
So what should I do to prevent it?
EDIT:
The controller is here:
#component = Component.find(params[:id])
respond_to do |format|
if #component.update_attributes(params[:component])
#component.update_attribute(:numImages, #component.assets.size)
#component.save
format.html { redirect_to #component, notice: 'Component was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #component.errors, status: :unprocessable_entity }
end
end
end
By the way, is correct to update :numImages as I do?
For the numImages update (your sub-question), I'd suggest using a before_update method in your Component model.
before_update :set_numimages
def set_numimages
numImages = assets.size
end
Also, you're calling update_attributes, update_attribute and save on #component. That invokes three save actions. I'd suggest you change it to this and see if the problem persists:
#component = Component.find(params[:id])
respond_to do |format|
if #component.update_attributes(params[:component])
format.html { redirect_to #component, notice: 'Component was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #component.errors, status: :unprocessable_entity }
end
end
I have a users table (Devise) , with a column "admin" (boolean) to qualify my users.
I also have a namespace "Backend".
I would like that my admins users can create new users from the backend namespace.
So I created a Backend::UsersController :
class Backend::UsersController < ApplicationController
layout 'admin'
before_filter :authenticate_user!
def index
#auteurs = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #auteurs }
end
end
def new
#auteur = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #auteur }
end
end
def create
#auteur = User.new(params[:auteur])
respond_to do |format|
if #auteur.save
format.html { redirect_to #auteur, notice: 'Article was successfully created.' }
format.json { render json: #auteur, status: :created, location: #auteur }
else
format.html { render action: "new" }
format.json { render json: #auteur.errors, status: :unprocessable_entity }
end
end
end
end
Here's the "_form" partial called in the "new" view :
<%= simple_form_for(#auteur) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
An here's my routes :
namespace :backend do
resources :articles
root to: "articles#index"
resources :accueil
resources :users
get "users/index"
get "users/create"
get "users/new"
end
devise_for :users
But when I try to access to "backend/users/new", it drives me to this error :
NoMethodError in Backend/users#new
Showing C:/Ruby/acturevue/app/views/backend/users/_form.html.erb where line #1 raised:
undefined method `users_path' for #<#<Class:0x39616b0>:0x416f6d0>
Does somebody have any ideas of the source of the problem ?
Thanks
UPDATE
So, I modified my code like that :
In Backend::UsersController :
def new
#user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #user}
end
end
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to backend_users_path(#user), notice: 'Article was successfully created.' }
format.json { render json: #user, status: :created, location: #user }
else
format.html { render action: "new" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
In _form :
<%= simple_form_for(#user) do |f| %>
But I still have the error.
However the backend_users_path exists in y routes :
backend_users GET /backend/users(.:format) backend/users#
index
POST /backend/users(.:format) backend/users#
create
new_backend_user GET /backend/users/new(.:format) backend/users#
new
edit_backend_user GET /backend/users/:id/edit(.:format) backend/users#
edit
backend_user GET /backend/users/:id(.:format) backend/users#
show
PUT /backend/users/:id(.:format) backend/users#
update
DELETE /backend/users/:id(.:format) backend/users#
destroy
You need to use the namespace in your redirect:
redirect_to [:backend, #auteur] #...
See my answer to this question.