HarryLucas
HarryLucas

Reputation: 159

Presetting a parameter in controller rails

Basically a user is on a project page where they click add new website, which is set out like this

<%= link_to 'Add Asset', new_website_path(:project_id => "#{@project.id}" ), class: 'btn btn-md btn-danger' %>

and then they fill out a form with some data. However I want it so that when they submit that form, that the project id (from the project page they were just on) gets saved under the project_id variable of the website but for some reason this isn't working.

class WebsitesController < ApplicationController
  def new
    @project = Project.find(params[:project_id])
    @website = Website.new(:type => 'Website', :project_id => @project)
  end

  def create
    @website = current_user.assets.build(website_params)

    if @website.save
      flash[:notice] = "Asset successfully added."
      redirect_to(:controller => 'projects', :action => 'show', :id => @website.project_id)
    else
      render(:action => 'new')
    end
  end

  private

  def website_params
    params.require(:website).permit(:id, :type,:url, :page_rank, :rev_company ,:social_pages)
  end
end

Is there something missing as to why this isn't working? When I check in the console, all the websites I add have a project_id of nil.

When i go onto the form in my browser this is the address:

http://localhost:3000/websites/new?project_id=1

Also, I think it is getting the right project ID because in the form I have a condition which looks at that project ID to see if it is of type website as in:

#views/websites/new.html.erb
<div class="col-md-5 col-md-offset-3">
    <div class="panel panel-default newasset">
        <div class="panel-heading">
            <h3 class="panel-title ptd"><%= "New #{@project.type}" %></h3>
        </div>
<div class='panel-body'>
            <div class='form group'>
<%= simple_form_for(@website, html: {class: 'form-group' }) do |f| %>
<% if (@project.type == 'Website') %>
<div class="col-xs-4 col-xs-offset-3">
  <%= f.input :title, label: 'Website Name' %>
  <%= f.input :url %>
  <%= f.input :rev_company, label: "Revenue Company" %>
  <%= f.input :social_pages, label: "Number of Social Sites" %>
  <%= f.input :page_rank %><br />
  <div class="form-actions">
  <%= f.button :submit, class: 'btn btn-primary' %>
</div>
<% else %>
You should not see this
<% end %>
<% end %>


        </div>
        </div>
    </div>
</div>

Upvotes: 1

Views: 55

Answers (2)

Kirti Thorat
Kirti Thorat

Reputation: 53018

Assuming that you are passing :project_id correctly then all you need to permit project_id in website_params method.

def website_params
  params.require(:website).permit(:id, :project_id, :type,:url, :page_rank, :rev_company ,:social_pages)
  ##                                     ^
  ##                                     Permit project_id 
end

Currently, you have not permitted the field project_id which is why its not getting saved.

For Rails 4+ (with Strong Parameters) you need to explicitly specify the fields which you would like to save in database.

Upvotes: 3

Jorge de los Santos
Jorge de los Santos

Reputation: 4633

@project is an object, and the database must be expecting an integer. You should call:

@project.id

The best solution anyway is to do:

def new
  @project = Project.find(params[:project_id]
  @website = @project.websites.build(:type => 'Website')
end

To allow rails to do his magick association through the models if you have declared the: belongs, and has_one/many

Upvotes: 0

Related Questions